Skip to main content

条件Condition

huhxAbout 1 minjavaConcurrency-ToolkitConcurrency

Condition将Object监视器方法(wait、notify和notifyAll)分解成截然不同的对象,以便通过将这些对象与任意Lock实现组合使用,为每个对象提供多个等待set(wait-set)。其中,Lock替代了synchronized方法和语句的使用,Condition替代了Object监视器方法的使用。今天我们就通过实例来学习一个Condition的用法。

使用

这里列举一个关于装水取水的例子:

BoundedBuffer
class BoundedBuffer {
    final Lock lock = new ReentrantLock();
    final Condition notFull = lock.newCondition();
    final Condition notEmpty = lock.newCondition();

    final String[] items = new String[10];
    int putptr, takeptr, count;

    public void put(String x) throws InterruptedException {
        lock.lock();
        try {
            while (count == items.length) notFull.await();
            items[putptr] = x;
            if (++putptr == items.length) putptr = 0;
            ++count;
            notEmpty.signal();
        } finally {
            lock.unlock();
        }
    }

    public String take() throws InterruptedException {
        lock.lock();
        try {
            while (count == 0) notEmpty.await();
            var x = items[takeptr];
            if (++takeptr == items.length) takeptr = 0;
            --count;
            notFull.signal();
            return x;
        } finally {
            lock.unlock();
        }
    }
}

运行的结果如下:不固定

p0 t0 t1 p1 p2 p3 p4 t2 t3 p5 p6 p7 t4 t5 p8 t6 p9 p10 t7 p11 t8 p12 t9 t10 t11 t12 t13 p13 p14 p15 t14 p16 t15 p17 t16 p18 t17 p19 t18 t19

分析

FAQ

总结

  • Condition可以替代wait和notify,Condition通过Lock对象获取
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();

参考