Java线程间通信-等待唤醒机制

线程之间的关系是平等的,彼此之间并不存在任何依赖,它们各自竞争CPU资源,互不相让,并且还无条件地阻止其他线程对共享资源的异步访问。然而,也有很多现实问题要求不仅要同步的访问同一共享资源,而且线程间还彼此牵制,通过相互通信来向前推进。

下面程序将通过等待唤醒机制实现线程间通信。

package com.javase.thread;

public class InputOutputDemo {
	
	static boolean lockFlag = false;
	public static void main(String[] args) {
		Resource r = new Resource();
		new Thread(new Input(r)).start();
		new Thread(new Output(r)).start();
		
	}
}

class Resource{
	private String name;
	private String sex;
	public String getName() {
		return name;
	}
	public String getSex() {
		return sex;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

class Input implements Runnable{
	private Resource r ;
	boolean flag = false;
	public Input(Resource r){
		this.r=r;
	}
	public void run(){
		while(true){
			synchronized(r){
				if(InputOutputDemo.lockFlag){
					try {
						r.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if(flag){
					r.setName("alex");
					r.setSex("male");
					flag=false;
				}else{
					r.setName("dd");
					r.setSex("female");
					flag=true;
				}
				InputOutputDemo.lockFlag=true;
				r.notify();
			}
			
		}
	}
}

class Output implements Runnable{
	private Resource r;
	public Output(Resource r){
		this.r=r;
	}
	public void run(){
		while(true){
			synchronized(r){
				if(!InputOutputDemo.lockFlag){
					try {
						r.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println("name:"+r.getName()+"   sex:"+r.getSex());
				InputOutputDemo.lockFlag=false;
				r.notify();
			}
			
		}
	}
}

运行结果:

name:dd   sex:female
name:alex   sex:male
name:dd   sex:female
name:alex   sex:male
name:dd   sex:female
name:alex   sex:male
name:dd   sex:female

......


以下代码也可获得相同的运行结果:

package com.javase.thread;

public class InputOutputDemo {
	
	static boolean lockFlag = false;
	public static void main(String[] args) {
		Resource r = new Resource();
		new Thread(new Input(r)).start();
		new Thread(new Output(r)).start();
		
	}
}

class Resource{
	private String name;
	private String sex;
	boolean flag = false;
	public synchronized void set(String name , String sex){
		if(flag){
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.name=name;
		this.sex=sex;
		flag=true;
		this.notify();
	}
	public synchronized void print(){
		if(!flag){
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("name:"+name+"   sex:"+sex);
		flag=false;
		this.notify();
	}
	
}

class Input implements Runnable{
	private Resource r ;
	boolean flag = false;
	public Input(Resource r){
		this.r=r;
	}
	public void run(){
		while(true){
			if(flag){
				r.set("alex", "male");
				flag=false;
			}else{
				r.set("dd", "female");
				flag=true;
			}
			
		}
	}
}

class Output implements Runnable{
	private Resource r;
	public Output(Resource r){
		this.r=r;
	}
	public void run(){
		while(true){
			r.print();
		}
	}
}



你可能感兴趣的:(Java线程间通信-等待唤醒机制)