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线程
}
}