Java中Condition的使用

Java架构师交流群:793825326

java版本:jdk1.8

IDE:idea 18

之前的一篇博客(https://blog.csdn.net/dap769815768/article/details/96712257)讲了wait、notify\notifyAll的使用,实际上目前我们比较推荐的方式是用Condition的await、signal\signalAll来实现类似的功能。

添加一个测试类Test:

public class Test {
    private Lock lock=new ReentrantLock();
    private Condition condition=lock.newCondition();
    public void start() {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getId() + "执行");
            condition.await();
            System.out.println(Thread.currentThread().getId() + "start");
            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getId() + "end");
            lock.unlock();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
    public void releaseAll()
    {
        lock.lock();
        condition.signalAll();
        lock.unlock();
    }
    public synchronized void releaseOne()
    {
        lock.lock();
        condition.signal();
        lock.unlock();
    }
}

写一段测试代码:

Test t=new Test();
try {
    Thread thread1= new Thread(() ->  t.start());
    thread1.start();
    Thread.sleep(100);
    Thread thread2= new Thread(() ->  t.start());
    thread2.start();
    Thread.sleep(1000);
    t.releaseAll();
}
catch (Exception ex)
{
    System.out.println(ex);
}

这和之前使用wait、notify\notifyAll这套方案实现的效果一样。

如果单纯从这个例子来看,它们之间并没有什么区别,Condition的优势也没有体现出来。实际上Condition可以实现更细粒度的控制。看下面的代码:

public class Test {
    private Lock lock=new ReentrantLock();
    private Condition condition1=lock.newCondition();
    private Condition condition2=lock.newCondition();

    public void start1() {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getId() + "执行");
            condition1.await();
            System.out.println(Thread.currentThread().getId() + "start");
            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getId() + "end");
            lock.unlock();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public void start2() {
        try
        {
            lock.lock();
            System.out.println(Thread.currentThread().getId() + "执行");
            condition2.await();
            System.out.println(Thread.currentThread().getId() + "start");
            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getId() + "end");
            lock.unlock();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public void releaseAll()
    {
        lock.lock();
        condition1.signalAll();
        condition2.signalAll();
        lock.unlock();
    }
    public synchronized void release1()
    {
        lock.lock();
        condition1.signal();
        lock.unlock();
    }
    public synchronized void release2()
    {
        lock.lock();
        condition2.signal();
        lock.unlock();
    }
}

测试代码如下:

Test t=new Test();
try {
    Thread thread1= new Thread(() ->  t.start1());
    thread1.start();
    Thread.sleep(100);
    Thread thread2= new Thread(() ->  t.start2());
    thread2.start();
    Thread.sleep(1000);
    t.release2();
    t.release1();
}
catch (Exception ex)
{
    System.out.println(ex);
}

它的执行结果:

14执行
15执行
15start
15end
14start
14end

在这个例子中,采用Condition的方式,显然比之前控制地更精确了。

 

你可能感兴趣的:(Java中Condition的使用)