/** * @ClassName Question09 * @Description: 经典生产者消费者问题 * @Author xtanb * @Date 2019/10/21 * @Version V1.0 **/ public class Question09 { class CubbyHole{ private int seq; private boolean able = false; public synchronized int get(){ while(!able){ try { wait(); } catch (Exception e) { e.printStackTrace(); } } able=false; notify(); return seq; } public synchronized void put(int value){ while(able){ try { wait(); } catch (Exception e) { e.printStackTrace(); } } seq =value; able = true; notify(); } } class Producer extends Thread{ private CubbyHole cubbyHole; private int number; public Producer(CubbyHole c,int number){ cubbyHole= c; this.number=number; } public void run(){ for(int i=0;i<10;i++){ cubbyHole.put(i); System.out.println("Productor"+number +" put "+i); try { sleep((int)Math.random()*100); } catch (Exception e) { e.printStackTrace(); } } } } class Consumer extends Thread{ private CubbyHole cubbyHole; private int number; public Consumer(CubbyHole cubbyHole, int number) { super(); this.cubbyHole = cubbyHole; this.number = number; } public void run(){ int value =0; for(int i=0;i<10;i++){ value= cubbyHole.get(); System.out.println("Customer"+number+" get "+value ); } } } public static void main(String[] args) { CubbyHole cubbyHole = new Question09().new CubbyHole(); Producer p1 =new Question09().new Producer(cubbyHole, 1); Consumer c1 =new Question09().new Consumer(cubbyHole, 1); p1.start(); c1.start(); } }
2.0 reentranlock
package com.example.demo.study.questions; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @ClassName Question15 * @Description: 生产者消费者问题 * @Author xtanb * @Date 2019/10/22 * @Version V1.0 **/ class CubbyHoles{ private volatile boolean flag = true; private Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); private int num = 1; public void produce(){ try{ lock.lock(); while (!flag){ condition.await(); } System.out.println("生产"+num); flag = false; condition.signal(); }catch (Exception e){ e.printStackTrace(); }finally { lock.unlock(); } } public void consumer(){ try{ lock.lock(); while (flag){ try{ condition.await(); }catch (Exception e){ e.printStackTrace(); } } System.out.println("消费"+num); num++; flag = true; condition.signal(); }catch (Exception e){ e.printStackTrace(); }finally { lock.unlock(); } } } class Producers extends Thread{ private CubbyHoles cubbyHole; private int number; public Producers(CubbyHoles c,int number){ cubbyHole= c; this.number=number; } public void run(){ for(int i=0;i<10;i++){ cubbyHole.produce(); try { sleep((int)Math.random()*100); } catch (Exception e) { e.printStackTrace(); } } } } class Consumers extends Thread{ private CubbyHoles cubbyHole; private int number; public Consumers(CubbyHoles cubbyHole, int number) { super(); this.cubbyHole = cubbyHole; this.number = number; } public void run(){ for(int i=0;i<10;i++){ cubbyHole.consumer(); } } } public class Question15 { public static void main(String[] args) { CubbyHoles cubbyHoles = new CubbyHoles(); new Producers(cubbyHoles,1).start(); new Consumers(cubbyHoles,1).start(); } }