码迷,mamicode.com
首页 > 其他好文 > 详细

多生产者多消费者问题

时间:2017-10-19 00:19:37      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:inter   cat   pack   set   int   current   lock   over   string   

package test;

/**
 * 多生产者 多消费者问题
 * 
 * 该代码存在死锁问题
 * 因为有可能唤醒本方 
 * @author 黄二狗
 *
 */

public class Test {
     public static void main(String[] args) {
		Resource r = new Resource();
		Producer producer = new Producer(r);
		Consumer consumer = new Consumer(r);
		Thread t0 = new Thread(producer);
		Thread t1 = new Thread(producer);
		Thread t2 = new Thread(consumer);
		Thread t3 = new Thread(consumer);
		t0.start();
		t1.start();
		t2.start();
		t3.start();
	}
}

class Resource {
	private String name;
	private int count;
	private boolean flag;
	
	public synchronized void set(String name) throws InterruptedException {
		while(flag) 
			this.wait();  // t0 t1 
		this.name = name + count;
		count++;     
		System.out.println(Thread.currentThread().getName() + "....生产者..." + this.name);
		flag = true;
		this.notify();
	}
	
	public synchronized void out() throws InterruptedException {
		while(!flag)    //t2 t3
			this.wait();  
		System.out.println(Thread.currentThread().getName() + "....消费者..." + this.name);
		flag = false;
		this.notify();
	}
}

class Producer implements Runnable {
	 private Resource r;
	 public Producer(Resource r) {
		 this.r = r;
	 }
	@Override
	public void run() {
	   while(true) {
		   try {
			r.set("烤鸭");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }	
	}
}

class Consumer implements Runnable {
	 private Resource r;
	 public Consumer(Resource r) {
		 this.r = r;
	 }
	@Override
	public void run() {
		try {
			while(true) {
			  r.out();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  改进方法:将notify()换成notifyAll(),这样的话就不会产生死锁了.

 

package test;

/**
 * 多生产者 多消费者问题
 * 
 * 该代码存在死锁问题
 * 因为有可能唤醒本方 
 * @author 黄二狗
 *
 */

public class Test {
     public static void main(String[] args) {
		Resource r = new Resource();
		Producer producer = new Producer(r);
		Consumer consumer = new Consumer(r);
		Thread t0 = new Thread(producer);
		Thread t1 = new Thread(producer);
		Thread t2 = new Thread(consumer);
		Thread t3 = new Thread(consumer);
		t0.start();
		t1.start();
		t2.start();
		t3.start();
	}
}

class Resource {
	private String name;
	private int count;
	private boolean flag;
	
	public synchronized void set(String name) throws InterruptedException {
		while(flag) 
			this.wait();  // t0 t1 
		this.name = name + count;
		count++;     
		System.out.println(Thread.currentThread().getName() + "....生产者..." + this.name);
		flag = true;
		this.notifyAll();
	}
	
	public synchronized void out() throws InterruptedException {
		while(!flag)    //t2 t3
			this.wait();  
		System.out.println(Thread.currentThread().getName() + "....消费者..." + this.name);
		flag = false;
		this.notifyAll();
	}
}

class Producer implements Runnable {
	 private Resource r;
	 public Producer(Resource r) {
		 this.r = r;
	 }
	@Override
	public void run() {
	   while(true) {
		   try {
			r.set("烤鸭");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	   }	
	}
}

class Consumer implements Runnable {
	 private Resource r;
	 public Consumer(Resource r) {
		 this.r = r;
	 }
	@Override
	public void run() {
		try {
			while(true) {
			  r.out();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  

 

多生产者多消费者问题

标签:inter   cat   pack   set   int   current   lock   over   string   

原文地址:http://www.cnblogs.com/bean/p/7689908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!