Condition

Posted 天涯过者

tags:

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

Lock,那是对synchronized的一种更为面向对象的替代,在原来的synchronized内部,我们可以调用object的wait与notify方法

condition一方面是对lock功能的补充(也就是说,你用了lock,为了保证线程的通信,就得用condition)

另一方面,synchronized的notifyall是唤醒所有等待的线程,那么如果有些线程我不想唤醒呢。

下面的例子:主线程运行10次,然后子线程2运行20次,接着子线程3运行30次

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

public class ThreeConditionCommunication {


    /**
     * @param args
     */
    public static void main(String[] args) {

        final Business business = new Business();
        new Thread(
                new Runnable() {

                    @Override
                    public void run() {

                        for (int i = 1; i <= 4; i++) {
                            business.sub2(i);
                        }

                    }
                }
        ).start();

        new Thread(
                new Runnable() {

                    @Override
                    public void run() {

                        for (int i = 1; i <= 4; i++) {
                            business.sub3(i);
                        }

                    }
                }
        ).start();

        for (int i = 1; i <= 4; i++) {
            business.main(i);
        }

    }


    static class Business {
        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();
        private int shouldSub = 1;

        public void sub2(int i) {
            lock.lock();
            try {
                while (shouldSub != 2) {
                    try {
                        condition2.await();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                for (int j = 1; j <= 20; j++) {
                    System.out.println("sub2 thread sequence of " + j + ",loop of " + i);
                }
                shouldSub = 3;
                condition3.signal();
            } finally {
                lock.unlock();
            }
        }


        public void sub3(int i) {
            lock.lock();
            try {
                while (shouldSub != 3) {
                    try {
                        condition3.await();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                for (int j = 1; j <= 30; j++) {
                    System.out.println("sub3 thread sequence of " + j + ",loop of " + i);
                }
                shouldSub = 1;
                condition1.signal();
            } finally {
                lock.unlock();
            }
        }

        public void main(int i) {
            lock.lock();
            try {
                while (shouldSub != 1) {
                    try {
                        condition1.await();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                for (int j = 1; j <= 10; j++) {
                    System.out.println("main thread sequence of " + j + ",loop of " + i);
                }
                shouldSub = 2;
                condition2.signal();
            } finally {
                lock.unlock();
            }
        }

    }
}

 

以上是关于Condition的主要内容,如果未能解决你的问题,请参考以下文章

java线程condition

Java ReEntrantLock 之 Condition条件(Java代码实战-002)

Condition源码分析

if(!!condition) 和 if(condition) 有啥区别

[C]if (CONDITION)语句中CONDITION的情况

VBA 使用“或”条件执行直到循环