Java 线程的通讯--生产者和消费者

 

 

package 生产者和消费者;
//消费者
public class Customer implements Runnable {
	private Share_resources  rescources=null;
	public Customer( Share_resources  rescources){
		this.rescources=rescources;
	}
	public void run() {
		for (int i = 0; i <50; i++) {
			rescources.popup();
		}
	}

}

 

package 生产者和消费者;
//生产者
public class Producer implements Runnable{
	private Share_resources  rescources=null;
	public Producer( Share_resources  rescources){
		this.rescources=rescources;
	}
	//共享资源对象
	public void run() {
		for (int i = 0; i <50; i++) {
			if(i%2==0){
				rescources.push("张三", "男");
			}else{
				rescources.push("小花", "女");
			}
		}
	}
}

 

package 生产者和消费者;
//共享资源对象(姓名,性别)
public class Share_resources {
	private String name;
	private String  gender;
	private Boolean isEmpty=true;//表示共享资源是否为空状态
	/**
	 * 生产者向共享资源对象存储数据
	 * @param name 存储的姓名
	 * @param gender 存储的性别
	 */
	synchronized public void push(String name,String gender){
		while( ! isEmpty){//当前isEmpty为false的时候,不空等着消费者来获取
			try {
				this.wait();//使用同步锁调用,表示当前线程释放同步锁,进入等待池,只能被其他线程唤醒
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//生产开始-----------------------------
		this.name=name;
		this.gender=gender;
		//生产结束-----------------------------
		isEmpty=false;//设置共享资源不能为空
		this.notify();//唤醒一个消费者
	}
	/**
	 * 消费者从共享资源中取出数据
	 */
	synchronized public void popup(){
		try {
			while( isEmpty){//当前isEmpty为true的时候,不空等着生产者来生产
				this.wait();
			}
             //消费开始------------------------------------
			Thread.sleep(10);
			System.out.println(this.name+"---"+this.gender);
			//消费结束-------------------------------------
			isEmpty=true;
			this.notify();//唤醒生产者
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

package 生产者和消费者;

public class App {
	public static void main(String[] args) {
		//创建生产者和消费者共同的对象
		Share_resources  resources  =new Share_resources();
		//启动生产者线程
		new Thread(new Producer(resources)).start();
		//启动消费者线程
		new Thread(new Customer(resources)).start();
	}
}

 Java5生产者和消费者模式同步锁机制

package java5_同步锁机制;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//共享资源对象(姓名,性别)
public class Share_resources {
	private String name;
	private String  gender;
	private Boolean isEmpty=true;//表示共享资源是否为空状态
	private final Lock lock=new ReentrantLock();
	private Condition condition =lock.newCondition();
	/**
	 * 生产者向共享资源对象存储数据
	 * @param name 存储的姓名
	 * @param gender 存储的性别
	 */
 public void push(String name,String gender){
		lock.lock();//获取锁
		while( ! isEmpty){//当前isEmpty为false的时候,不空等着消费者来获
			try {
				condition.await();
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}finally {
				lock.unlock();//释放锁
			}
		}
		//生产开始-----------------------------
		this.name=name;
		this.gender=gender;
		//生产结束-----------------------------
		isEmpty=false;//设置共享资源不能为空
		condition.signal();//唤醒一个消费者
	}
	/**
	 * 消费者从共享资源中取出数据
	 */
public void popup(){
	lock.lock();//获取锁
		try {
			while( isEmpty){//当前isEmpty为true的时候,不空等着生产者来生产
				condition.await();
			}
             //消费开始------------------------------------
			Thread.sleep(10);
			System.out.println(this.name+"---"+this.gender);
			//消费结束-------------------------------------
			isEmpty=true;
			condition.signal();//唤醒生产者
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			lock.unlock();//释放锁
		}
	}
}

 

 

 

 

posted @ 2017-04-05 13:37  *小嘻嘻*  阅读(203)  评论(0编辑  收藏  举报