Fork me on GitHub

10. Condition 控制线程通信

1. 是什么 ?

2. 示例

package com.gf.demo09;

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

public class TestProductorAndConsumer {

	public static void main(String[] args) {
		Clerk clerk = new Clerk();
		
		Productor pro = new Productor(clerk);
		Consumer cus = new Consumer(clerk);
		
		new Thread(pro,"生产者A").start();
		new Thread(cus,"消费者B").start();
		
		new Thread(pro,"生产者C").start();
		new Thread(cus,"消费者D").start();
	}
	
}

//店员
class Clerk{
	private int product = 0;
	
	private Lock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();
	
	//进货
	public void get(){
		lock.lock();
		
		while(1 <= product){ // if 改为 while 解决虚假唤醒问题,应该总是使用在循环中
			System.out.println("产品已满!");
			try {
				condition.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println(Thread.currentThread().getName()+" : " + ++product);
		condition.signalAll();
	}
	
	//卖货
	public void sale(){
		lock.lock();
		
		while(0 >= product){ // if 改为 while 解决虚假唤醒问题,应该总是使用在循环中
			System.out.println("缺货!");
			try {
				condition.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println(Thread.currentThread().getName()+" : " + --product);
		condition.signalAll();
	}
}

//生成者
class Productor implements Runnable{
	
	private Clerk clerk;
	
	public Productor(Clerk clerk) {
		this.clerk = clerk;
	}

	@Override
	public void run() {
		for (int i = 0; i < 20; i++) {
			try {
				Thread.sleep(200); // 模拟网络延迟
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			clerk.get();
		}
	}
	
}

//消费者
class Consumer implements Runnable{
	
	private Clerk clerk;
	
	public Consumer(Clerk clerk) {
		this.clerk = clerk;
	}

	@Override
	public void run() {
		for (int i = 0; i < 20; i++) {
			clerk.sale();
		}
	}
	
}

 

关注我的公众号,精彩内容不能错过

posted @ 2017-12-14 15:00  程序员果果  阅读(145)  评论(0编辑  收藏  举报