`

生产者消费者模式(一)

    博客分类:
  • JDK
 
阅读更多
需求: 多个生产者不断的生产产品,多个消费者不断的消费产品,仓库可以存放10个产品。 第一批产品需要生产20个产品,并消费完毕。

结论: 当使用一个生产者,一个消费者是,需要4610,使用7个生产者,7个消费者时,只需要900,可见当N个任务耗时长,使用多线程可以充分发挥多核CPU的优点.
 package ycl.learn.effective.java.thread.pc;


/**
 * 公共资源类
 */
public class PublicResource {

	private int number = 0;
	
	private int index = 0;

	public static final int MAX_POOL = 10;
	
	public static final Product[] products = new Product[MAX_POOL];
 
	public static final int MAX_RESOURCE = 20;

	/**
	 * 增加公共资源
	 */
	public synchronized boolean increace() {
		if(isComplete()){//生产任务完成
			return false;
		}
		
		while (isFull()) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} 
		number++;
		index++; 
		Product p = new Product(index);
		products[number-1]=p;
		notifyAll(); 
		System.out.println(Thread.currentThread()+"increace:" + index); 
		return true;
	}

	/**
	 * 减少公共资源
	 */
	public synchronized boolean decreace() { 
		while (isEmpty()) {
			try {
				if(isComplete()){//如果消费结束,将会死锁,因为isEmpty()is true forever, 所以不管你如何notify,他始终等待,添加当wait被notify时,消费任务完成,退出。
					return false;
				} 
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}  
		Product p =products[number-1];
		number--;
		notifyAll(); 
		System.out.println(Thread.currentThread()+"decreace:" + p.index); 
		return true;
	}

	/**
	 * 判断是否满了
	 */
	public  boolean isFull() {
		return number >= MAX_POOL;
	}

	/**
	 * 判断是否空了
	 */
	public  boolean isEmpty() {
		return number <= 0;
	}

	/**
	 * 判断生产完毕 
	 *  
	 */
	public  boolean isComplete() {
		return index >= MAX_RESOURCE;
	}
	
	class Product{
		public int index; 
		public Product(int index){
			this.index = index;
		}
	}
}



package ycl.learn.effective.java.thread.pc;

public class Productor implements Runnable {

	private PublicResource ps; 

	public Productor(PublicResource ps) {
		this.ps = ps;
	}

	public void run() { 
		while (true) {
			boolean flag = ps.increace();
			if(!flag){
				System.out.println(Thread.currentThread()+" exist");
				break;
			}
			try {
				Thread.sleep((int) (Math.random() * 500));
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
		}
	}
}



package ycl.learn.effective.java.thread.pc;

public class Customer implements Runnable {

	private PublicResource ps;

	public Customer(PublicResource ps) {
		this.ps = ps;
	}

	public void run() {
		while (true) { 
			boolean flag = ps.decreace(); 
			if(!flag){
				System.out.println(Thread.currentThread()+" exist");
				break;
			}
			try {
				Thread.sleep((int) (Math.random() * 500));
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
		}
	}

}


package ycl.learn.effective.java.thread.pc;

public class TestPC {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		one();
	}

	public static void one() throws InterruptedException{
		long start = System.currentTimeMillis();
		PublicResource ps = new PublicResource();
		Customer c = new Customer(ps);
		Productor p = new Productor(ps);
		Thread ct = new Thread(c,"customer0");
		Thread ct1 = new Thread(c,"customer1");
		Thread ct2 = new Thread(c,"customer2");
		Thread ct3 = new Thread(c,"customer3");
		Thread ct4 = new Thread(c,"customer4");
		Thread ct5 = new Thread(c,"customer5");
		Thread ct6 = new Thread(c,"customer6");
		
		Thread pt = new Thread(p,"productor0");
		Thread pt1 = new Thread(p,"productor1");
		Thread pt2 = new Thread(p,"productor2");
		Thread pt3 = new Thread(p,"productor3");
		Thread pt4 = new Thread(p,"productor4");
		Thread pt5 = new Thread(p,"productor5");
		Thread pt6 = new Thread(p,"productor6");
		ct.start();
		ct1.start();
		ct2.start();
		ct3.start();
		ct4.start();
		ct5.start();
		ct6.start();
		
		pt.start();
		pt1.start();
		pt2.start();
		pt3.start();
		pt4.start();
		pt5.start();
		pt6.start();
		 
		
		ct.join();
		ct1.join();
		ct2.join();
		ct3.join();
		ct4.join();
		ct5.join();
		ct6.join();
		
		pt.join();
		pt1.join();
		pt2.join();
		pt3.join();
		pt4.join();
		pt5.join();
		pt6.join();
		System.out.println(System.currentTimeMillis()-start);//4968
	}
}



Thread[productor0,5,main]increace:1
Thread[customer5,5,main]decreace:1
Thread[productor2,5,main]increace:2
Thread[customer0,5,main]decreace:2
Thread[productor0,5,main]increace:3
Thread[customer4,5,main]decreace:3
Thread[productor1,5,main]increace:4
Thread[productor4,5,main]increace:5
Thread[customer1,5,main]decreace:5
Thread[customer3,5,main]decreace:4
Thread[productor3,5,main]increace:6
Thread[customer6,5,main]decreace:6
Thread[productor6,5,main]increace:7
Thread[customer2,5,main]decreace:7
Thread[productor5,5,main]increace:8
Thread[customer6,5,main]decreace:8
Thread[productor2,5,main]increace:9
Thread[customer3,5,main]decreace:9
Thread[productor4,5,main]increace:10
Thread[customer4,5,main]decreace:10
Thread[productor6,5,main]increace:11
Thread[customer5,5,main]decreace:11
Thread[productor1,5,main]increace:12
Thread[customer0,5,main]decreace:12
Thread[productor1,5,main]increace:13
Thread[customer2,5,main]decreace:13
Thread[productor6,5,main]increace:14
Thread[customer3,5,main]decreace:14
Thread[productor1,5,main]increace:15
Thread[customer1,5,main]decreace:15
Thread[productor1,5,main]increace:16
Thread[customer2,5,main]decreace:16
Thread[productor5,5,main]increace:17
Thread[customer3,5,main]decreace:17
Thread[productor2,5,main]increace:18
Thread[customer4,5,main]decreace:18
Thread[productor0,5,main]increace:19
Thread[customer4,5,main]decreace:19
Thread[productor3,5,main]increace:20
Thread[productor5,5,main] exist
Thread[productor4,5,main] exist
Thread[customer6,5,main]decreace:20
Thread[productor1,5,main] exist
Thread[customer1,5,main] exist
Thread[customer5,5,main] exist
Thread[productor0,5,main] exist
Thread[customer0,5,main] exist
Thread[customer4,5,main] exist
Thread[productor6,5,main] exist
Thread[productor2,5,main] exist
Thread[customer6,5,main] exist
Thread[productor3,5,main] exist
Thread[customer2,5,main] exist
Thread[customer3,5,main] exist
875




  • 大小: 43.5 KB
分享到:
评论
2 楼 a123159521 2014-04-10  
putonyuer 写道
画图用了多长时间?》

what do you care?
1 楼 putonyuer 2014-04-07  
画图用了多长时间?》

相关推荐

Global site tag (gtag.js) - Google Analytics