Lock和Condition实现生产者和消费者

Posted moris5013

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lock和Condition实现生产者和消费者相关的知识,希望对你有一定的参考价值。

一个生产者和一个消费者

public class ConditionTest {

    private static ReentrantLock lock = new ReentrantLock();

    private static Condition condition = lock.newCondition();

    private static int data = 0;

    private  static  volatile  boolean noUse = false;

    private  static  void  produceData(){
        try{
            lock.lock();
            while (noUse){
                condition.await();
            }
            data++;
            System.out.println("produce data "+data);
            Thread.sleep(1000);
            noUse = true;
            condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
           lock.unlock();
        }
    }

    private  static  void consumeData(){
        try{
            lock.lock();
            while (!noUse){
                condition.await();
            }
            Thread.sleep(1000);
            System.out.println("consume data "+data);
            noUse = false;
            condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
    public static void main(String[] args) {
        new Thread(() -> {
           for (;;){
               produceData();
           }
        }).start();

        new Thread(() -> {
            for (;;){
                consumeData();
            }
        }).start();
    }
}

多个生产者和多个消费者

public class ConditionTest2 {

    private static Lock lock = new ReentrantLock();

    private static Condition PRODUCE_COND = lock.newCondition();

    private static Condition CONSUNME_COND = lock.newCondition();

    private  static LinkedList<String> list = new LinkedList<String>();

    private  static  final  int  MAX_CAPACITY = 10;

    private  static AtomicInteger counter = new AtomicInteger(1);

    private  static  void produce(){
        try{
            lock.lock();
            while (list.size() >= MAX_CAPACITY){
                PRODUCE_COND.await();
            }
            String value = "data:"+counter.getAndIncrement();
            System.out.println(Thread.currentThread().getName()+" produce "+value);
            list.addLast(value);
            CONSUNME_COND.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private  static  void consume(){
        try{
            lock.lock();
            while (list.isEmpty()){
                CONSUNME_COND.await();
            }
            String value = list.removeFirst();
            System.out.println(Thread.currentThread().getName()+" consume "+value);
            PRODUCE_COND.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        for(int i=1; i<5; i++){
            new Thread(()->{
                for(;;){
                    produce();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"生产者"+i).start();
        }

        for(int i=1; i<2; i++){
            new Thread(()->{
                for(;;){
                    consume();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"消费者"+i).start();
        }

    }
}

 

 

以上是关于Lock和Condition实现生产者和消费者的主要内容,如果未能解决你的问题,请参考以下文章

通过Lock对象以及Condition对象实现多线程同步

Lock+Condition实现机制

java并发编程Lock & Condition 协调同步生产消费

生产者和消费者及8锁现象

Lock锁与Condition监视器(生产者与消费者)。

java使用lock实现一个简单的生产者和消费者模式