java, presentation
Condition只出现在java1.5中,主要用来代替传统的对象的wait()和notify()实现线程间的协作。与对象的wait()和notify()相比,
使用条件的await()和signal()来实现线程间的协作会更加安全高效。
所以一般建议使用Condition,阻塞队列实际上是用Condition来模拟线程间的协作。
Condition实际上是一个接口,基本方法是await()和signal()。
条件依赖于锁接口,生成条件的基本代码,即lock.newCondition()。
如果要调用条件的await()和signal()方法,必须用lock保护,也就是说必须在lock.lock()和lock.unlock之间使用。
描述:
1. The wait () in the condition corresponds to the wait () of the target;
2. The signal () in the condition corresponds to the notify () of the target;
3. All signals in the condition () correspond to the notifyAll () of the target.
需要注意的是,条件的执行方式是,在线程消费者中调用await方法时,线程消费者会释放锁,自己休眠,等待被唤醒,线程生产者获得锁后,
它会开始运行,运行完毕后会调用条件的signalall方法唤醒线程消费者,线程消费者就可以恢复执行了。
java,以上就是本文为您收集整理的java最新内容,希望能帮到您!更多相关内容欢迎关注。