1.禁用cpu緩存,防止多線程工作時共用變量而獲知變量值不及時。
2.

class WashKettle implements Runnable{
public void run(){
System.out.println("洗水壺中~");
try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
class BoilWater implements Runnable{
public void run(){
for(int i=1;i<16;i++){
System.out.println("燒水的第"+i+"分鐘~");
try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MakeTea implements Runnable{
public void run() {
System.out.println("泡茶啦~");
try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("泡完了!");
}
}
class WashTeapot implements Runnable{
public void run(){
System.out.println("洗茶壺!");
try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
class WashTeacup implements Runnable{
public void run(){
for(int i=1;i<3;i++){
System.out.println("洗茶杯的第"+i+"分鐘~");
try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class GetTea implements Runnable{
public void run(){
System.out.println("拿茶葉(~ ̄▽ ̄)~ ");
try{
Thread.sleep(100);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public class Tea{
public static void main(String[]args) {
WashKettle washKettle=new WashKettle();
BoilWater boilWater=new BoilWater();
MakeTea makeTea=new MakeTea();
WashTeapot washTeapot=new WashTeapot();
WashTeacup washTeacup=new WashTeacup();
GetTea getTea=new GetTea();
Thread t1=new Thread (washKettle);
Thread t2=new Thread (boilWater);
Thread t3=new Thread (makeTea);
Thread t4=new Thread (washTeapot);
Thread t5=new Thread (washTeacup);
Thread t6=new Thread (getTea);
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
t4.start();
try {
t4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t5.start();
try {
t5.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t6.start();
try {
t6.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t3.start();
}
}
仿15.4
/*SeverSocket*/
import java.io.*;
import java.net.*;
public class MultiTalkServer{
static int clientnum=0; //靜态成員變量,記錄目前客戶的個數
public static void main(String args[]) throws IOException {
ServerSocket serverSocket=null;
boolean listening=true;
try{
//建立一個ServerSocket在端口4700監聽客戶請求
serverSocket=new ServerSocket(4700);
}catch(IOException e) {
System.out.println("Could not listen on port:4700.");
System.exit(-1); //退出
}
while(listening){ //循環監聽
//監聽到客戶請求,根據得到的Socket對象和客戶計數建立服務線程,并啟動之
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++; //增加客戶計數
}
serverSocket.close(); //關閉ServerSocket
}
}
/*MultiTalkSocket*/
package MultiSocket;
import java.io.*;
import java.net.*;
public class MultiTalkServer{
static int clientnum=0; //靜态成員變量,記錄目前客戶的個數
public static void main(String args[]) throws IOException {
ServerSocket serverSocket=null;
boolean listening=true;
try{
//建立一個ServerSocket在端口4700監聽客戶請求
serverSocket=new ServerSocket(4700);
}catch(IOException e) {
System.out.println("Could not listen on port:4700.");
System.exit(-1); //退出
}
while(listening){ //循環監聽
//監聽到客戶請求,根據得到的Socket對象和客戶計數建立服務線程,并啟動之
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++; //增加客戶計數
}
serverSocket.close(); //關閉ServerSocket
}
}
/*MultiSocket*/
package MultiSocket;
import java.io.*;
import java.net.*;
public class MultiTalkServer{
static int clientnum=0; //靜态成員變量,記錄目前客戶的個數
public static void main(String args[]) throws IOException {
ServerSocket serverSocket=null;
boolean listening=true;
try{
//建立一個ServerSocket在端口4700監聽客戶請求
serverSocket=new ServerSocket(4700);
}catch(IOException e) {
System.out.println("Could not listen on port:4700.");
System.exit(-1); //退出
}
while(listening){ //循環監聽
//監聽到客戶請求,根據得到的Socket對象和客戶計數建立服務線程,并啟動之
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++; //增加客戶計數
}
serverSocket.close(); //關閉ServerSocket
}
}
仿15.5
//用戶端
import java.io.*;
import java.net.*;
public class QuoteClient {
public static void main(String[] args) throws IOException{
DatagramSocket socket=new DatagramSocket();//建立資料報套接字
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
String readLine;
InetAddress address=InetAddress.getByName("127.0.0.1");//Server的IP資訊
while(!(readLine = sin.readLine()).equals("bye")) {
byte[] buf = readLine.getBytes();
//建立DatagramPacket對象
DatagramPacket packet=new DatagramPacket(buf, buf.length, address, 4445);
socket.send(packet); //發送
buf = new byte[256];
//建立新的DatagramPacket對象,用來接收資料報
packet=new DatagramPacket(buf,buf.length);
socket.receive(packet); //接收
buf = packet.getData();
//根據接收到的位元組數組生成相應的字元串
String received=new String(buf);
//列印生成的字元串
System.out.println("Quote of the Sever: "+received );
}
socket.close(); //關閉套接口
}
}
//服務端+線程
import java.io.*;
import java.net.*;
class QuoteServerThread extends Thread//伺服器線程
{
protected DatagramSocket socket=null;//記錄和本對象相關聯的DatagramSocket對象
protected BufferedReader in=null;//用來讀檔案的一個Reader
protected boolean moreQuotes=true;//标志變量,是否繼續操作
public QuoteServerThread() throws IOException {//無參數的構造函數
this("QuoteServerThread");//以QuoteServerThread為預設值調用帶參數的構造函數
}
public QuoteServerThread(String name) throws IOException {
super(name); //調用父類的構造函數
socket=new DatagramSocket(4445);//在端口4445建立資料報套接字
in= new BufferedReader(new InputStreamReader(System.in));
}
public void run() //線程主體
{
while(moreQuotes) {
try{
byte[] buf=new byte[256]; //建立緩沖區
DatagramPacket packet=new DatagramPacket(buf,buf.length);
//由緩沖區構造DatagramPacket對象
socket.receive(packet); //接收資料報
//列印出用戶端發送的内容
System.out.println("Client : "+new String(packet.getData()));
//從螢幕擷取輸入内容,作為發送給用戶端的内容
String dString= in.readLine();
//如果是bye,則向用戶端發完消息後退出
if(dString.equals("bye")){moreQuotes=false;}
buf=dString.getBytes();//把String轉換成位元組數組,以便傳送
//從Client端傳來的Packet中得到Client位址
InetAddress address=packet.getAddress();
int port=packet.getPort(); //端口号
//根據用戶端資訊建構DatagramPacket
packet=new DatagramPacket(buf,buf.length,address,port);
socket.send(packet); //發送資料報
}catch(IOException e) { //異常處理
e.printStackTrace(); //列印錯誤棧
moreQuotes=false; //标志變量置false,以結束循環
}
}
socket.close(); //關閉資料報套接字
}
}
public class QuoteServer{
public static void main(String args[]) throws java.io.IOException
{
new QuoteServerThread().start();//啟動一個QuoteServerThread線程
}
}