分为三种方式实现,具体见代码注释:
package beijing.lele; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ProduceConsumeMoreThread { /** * 使用 Lock + Condition类 实现 * 在使用 第一种方式的notifyAll中,由于notifyAll唤醒的是己方和对方线程,优化的是只唤醒对方进程 * */ public static void main(String[] args) { //Resources rource =new Resources(); 使用 notifyAll方式 实现多生产多消费 //ResourcesCondition rource =new ResourcesCondition(); 使用 Lock Condition方式 ResourcesConditionMore rource =new ResourcesConditionMore(); // 使用改进版 Lock Condition方式 ProductorMore pro = new ProductorMore(rource); ConsumerMore con = new ConsumerMore(rource); Thread t1 =new Thread(pro,"张大厨"); Thread t2 =new Thread(pro,"李大厨");//多个生产者 Thread t3 =new Thread(con,"学生甲"); Thread t4 =new Thread(con,"学生乙");//多个消费者 t1.start(); t2.start(); t3.start(); t4.start(); System.out.println("Hello World!"); } } class ConsumerMore implements Runnable { private Resources res; private ResourcesCondition res1; private ResourcesConditionMore res2; ConsumerMore(Resources res){ this.res =res; } ConsumerMore(ResourcesCondition res1){ this.res1 =res1; } ConsumerMore(ResourcesConditionMore res){ this.res2 =res; } public void run(){ while(true){ //res.out(); //res1.out(); res2.out(); } } } class ProductorMore implements Runnable { private Resources res; private ResourcesCondition res1 ; ResourcesConditionMore res2; ProductorMore(Resources res){ this.res =res; } ProductorMore(ResourcesCondition res){ this.res1 =res; } ProductorMore(ResourcesConditionMore res){ this.res2 =res; } public void run(){ while(true){ //res.set("++商品++"); //res1.set("++商品++"); res2.set("++商品++"); } } } /** 这是这一版的跑的结果,可以看到 即使在 消费时长 张大厨生产者++商品++--1 学生甲 ....消费者....++商品++--1 张大厨生产者++商品++--2 学生甲 ....消费者....++商品++--2 张大厨生产者++商品++--3 学生甲 ....消费者....++商品++--3 张大厨生产者++商品++--4 学生乙 ....消费者....++商品++--4 张大厨生产者++商品++--5 学生乙 ....消费者....++商品++--5 张大厨生产者++商品++--6 学生乙 ....消费者....++商品++--6 张大厨生产者++商品++--7 学生乙 ....消费者....++商品++--7 张大厨生产者++商品++--8 学生乙 ....消费者....++商品++--8 张大厨生产者++商品++--9 学生乙 ....消费者....++商品++--9 张大厨生产者++商品++--10 学生乙 ....消费者....++商品++--10 张大厨生产者++商品++--11 学生甲 ....消费者....++商品++--11 张大厨生产者++商品++--12 学生甲 ....消费者....++商品++--12 李大厨生产者++商品++--13 学生甲 ....消费者....++商品++--13 李大厨生产者++商品++--14 学生甲 ....消费者....++商品++--14 * @author Administrator * */ class ResourcesCondition { private String name; private int count =1; private boolean flag =false; private Lock lock = new ReentrantLock(); // 替代 synchronized的并提供更强大的功能 private Condition condition = lock.newCondition(); public void set(String name) { lock.lock(); try { while(flag) { condition.await(); } Thread.sleep(200); this.name = name+"--"+count++; System.out.println(Thread.currentThread().getName()+"生产者"+this.name); flag = true; condition.signalAll(); // 唤醒 } catch (InterruptedException e1) { e1.printStackTrace(); }finally { lock.unlock(); } } public synchronized void out() { lock.lock(); try{ //1)循环判断 while(!flag){ condition.await(); } Thread.sleep(600); System.out.println(Thread.currentThread().getName()+" ....消费者...."+this.name); flag =false; condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); }// 模拟消费耗时 finally { lock.unlock(); } } } class ResourcesConditionMore { private String name; private int count =1; private boolean flag =false; private Lock lock = new ReentrantLock(); private Condition condition_pro = lock.newCondition();// 使用Lock建立生产者的condition对象 private Condition condition_consume = lock.newCondition();// 使用Lock建立消费者的condition对象 public void set(String name) { lock.lock(); try { while(flag) { condition_pro.await(); } Thread.sleep(200); this.name = name+"--"+count++; System.out.println(Thread.currentThread().getName()+"生产者"+this.name); flag = true; condition_consume.signal(); // 生产者生产完毕后,唤醒消费者的进程(不再是signalAll) } catch (InterruptedException e1) { e1.printStackTrace(); }finally { lock.unlock(); } } public synchronized void out() { lock.lock(); try{ //1)循环判断 while(!flag){ condition_consume.await(); } Thread.sleep(600); System.out.println(Thread.currentThread().getName()+" ....消费者...."+this.name); flag =false; condition_pro.signal(); // 消费者消费完毕后,唤醒生产者的进程 } catch (InterruptedException e) { e.printStackTrace(); }// 模拟消费耗时 finally { lock.unlock(); } } } class Resources { private String name; private int count =1; private boolean flag =false; public synchronized void set(String name) { //1)循环判断 while(flag) try{this.wait();}catch(Exception e){} this.name = name+"--"+count++; try { Thread.sleep(200) ; } catch (InterruptedException e) { e.printStackTrace(); }// 模拟生产耗时 System.out.println(Thread.currentThread().getName()+"生产者"+this.name); flag =true; //2)唤醒所有进程 this.notifyAll(); } public synchronized void out() { //1)循环判断 while(!flag) try{this.wait();}catch(Exception e){} try { Thread.sleep(200) ; } catch (InterruptedException e) { e.printStackTrace(); }// 模拟消费耗时 System.out.println(Thread.currentThread().getName()+" ....消费者...."+this.name); flag =false; //2)唤醒所有进程 this.notifyAll(); } }