Condition

在这里插入图片描述
借用网上一张图。我们知道AQS中对于抢占同步资源失败的线程,会加入到同步
队列中,这个同步队列是个双向链表,head表示获取到同步资源的线程。而下面
的condition1和condition2等成为条件队列,condition通过锁lock获得,当调用condition的await方法后,会将当前线程释放,并将当前线程加入到条件队列中,
当condition调用singal方法后,会将当前条件队列中的首节点线程加入同步队列
抢占同步资源,当condition调用singalAll方法后,会将当前条件队列中的所有节
点线程加入到同步队列抢占同步资源。

总结:其实就是线程在同步队列和条件队列之间来回倒转,只能在其中一个队列
中呆着。

题目:使用condition交替打印123,循环10次

public class ConditionTest {
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition1 = lock.newCondition();
    static Condition condition2 = lock.newCondition();
    static Condition condition3 = lock.newCondition();
    static volatile int  flag  = 1;

    public static void print1(){
        lock.lock();
        try {
            while (flag==1){
                flag = 2;
                System.out.println("1..."+Thread.currentThread().getName());
                condition1.await();
                condition2.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    private static void print2(){
        lock.lock();
        try {
            while (flag==2){
                flag=3;
                System.out.println("2..."+Thread.currentThread().getName());
                condition2.await();
                condition3.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    private static void print3(){
        lock.lock();
        try {
            while (flag==3){
                flag=1;
                System.out.println("3..."+Thread.currentThread().getName());
                System.out.println("-------------");
                condition3.await();
                condition1.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){
            Thread t1 = new Thread(()->{
                print1();
            },"name:"+i+" "+1);
            Thread t2 = new Thread(()->{
                print2();
            },"name:"+i+" "+2);
            Thread t3 = new Thread(()->{
                print3();
            },"name:"+i+" "+3);
            t1.start();
            t2.start();
            t3.start();
        }
    }
}

题目:实现生产者消费者模型

public class ConditionTest {
    private int storage;
    private int putCounter;
    private int getCounter;
    private Lock lock = new ReentrantLock();
    private Condition putCondition = lock.newCondition();
    private Condition getCondition = lock.newCondition();

    public void put() throws InterruptedException {
        try {
            lock.lock();
            if (storage > 0) {
                System.out.println("put wait");
                putCondition.await();
                System.out.println("put wait end ...");
            }
            storage++;
            System.out.println("put => " + ++putCounter );
            getCondition.signal();
        } finally {
            lock.unlock();
        }
    }

    public void get() throws InterruptedException {
        try {
            lock.lock();
            if (storage <= 0) {
                System.out.println("get wait...");
                getCondition.await();
                System.out.println("get wait end ...");
            }
            storage--;
            System.out.println("get  => " + ++getCounter);
            putCondition.signal();
        } finally {
            lock.unlock();
        }
    }

    public class PutThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 2; i++) {
                try {
                    put();
                } catch (InterruptedException e) {
                }
            }
        }
    }

    public class GetThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 2; i++) {
                try {
                    get();
                } catch (InterruptedException e) {
                }
            }
        }
    }

    public static void main(String[] args) {
        final ConditionTest test = new ConditionTest();
        Thread put = test.new PutThread();
        Thread get = test.new GetThread();
        put.start();
        get.start();
    }
}

运行结果控制台打印为:

put => 1
put wait
get  => 1
get wait...
put wait end ...
put => 2
get wait end ...
get  => 2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值