其他分享
首页 > 其他分享> > 14.线程通信阻塞队列版(lock, await, signal)

14.线程通信阻塞队列版(lock, await, signal)

作者:互联网

1.代码

package com.mydemo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TraditionalProducerConsumerDemo {
    public static void main(String[] args) {
        ShareData shareData = new ShareData();
        for (int i1 = 0; i1 < 5; i1++) {
            new Thread(()->{
                try {
                    shareData.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"t1").start();

        }
        for (int i1 = 0; i1 < 5; i1++) {
            new Thread(()->{
                try {
                    shareData.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"t2").start();

        }

    }
}

class ShareData{
    private int number=0;
    private Lock lock=new ReentrantLock();
    private Condition condition=lock.newCondition();
    public void increment() throws InterruptedException {
        lock.lock();
        while (number!=0)
        {
            condition.await();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"\t "+number);
        condition.signalAll();
        lock.unlock();

    }

    public void decrement() throws InterruptedException {
        lock.lock();
        while (number==0)
        {
            condition.await();
        }
        number-- ;
        System.out.println(Thread.currentThread().getName()+"\t "+number);
        condition.signalAll();
        lock.unlock();




    }






}

2.执行结果

t1     1
t2     0
t1     1
t2     0
t1     1
t2     0
t1     1
t2     0
t1     1
t2     0

 

标签:14,i1,lock,signal,t2,number,t1,condition
来源: https://www.cnblogs.com/upupup-999/p/16142563.html