Condition
来源:互联网 发布:淘宝正品足球鞋 编辑:程序博客网 时间:2024/04/27 16:48
Object Condition
休眠 wait await
唤醒个线程 notify signal
唤醒所有线程 notifyAll signalAll
Condition它更强大的地方在于:能够更加精细的控制多线程的休眠与唤醒。
对于同一个锁,我们可以创建多个Condition,就是多个监视器的意思。在不同的情况下使用不同的Condition。
例如,假如多线程读/写同一个缓冲区:当向缓冲区中写入数据之后,唤醒"读线程";当从缓冲区读出数据之后,唤醒"写线程";
如果采用Object类中的wait(), notify(), notifyAll()实现该缓冲区,当向缓冲区写入数据之后需要唤醒"读线程"时,不可能通过notify()或notifyAll()明确的指定唤醒"读线程",而只能通过notifyAll唤醒所有线程(但是notifyAll无法区分唤醒的线程是读线程,还是写线程)。
下面将之前写过的一个线程通信的例子替换成用Condition实现(Java线程(三)),代码如下:
- public class ThreadTest2 {
- public static void main(String[] args) {
- final Business business = new Business();
- new Thread(new Runnable() {
- @Override
- public void run() {
- threadExecute(business, "sub");
- }
- }).start();
- threadExecute(business, "main");
- }
- public static void threadExecute(Business business, String threadType) {
- for(int i = 0; i < 100; i++) {
- try {
- if("main".equals(threadType)) {
- business.main(i);
- } else {
- business.sub(i);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class Business {
- private boolean bool = true;
- private Lock lock = new ReentrantLock();
- private Condition condition = lock.newCondition();
- public /*synchronized*/ void main(int loop) throws InterruptedException {
- lock.lock();
- try {
- while(bool) {
- condition.await();//this.wait();
- }
- for(int i = 0; i < 100; i++) {
- System.out.println("main thread seq of " + i + ", loop of " + loop);
- }
- bool = true;
- condition.signal();//this.notify();
- } finally {
- lock.unlock();
- }
- }
- public /*synchronized*/ void sub(int loop) throws InterruptedException {
- lock.lock();
- try {
- while(!bool) {
- condition.await();//this.wait();
- }
- for(int i = 0; i < 10; i++) {
- System.out.println("sub thread seq of " + i + ", loop of " + loop);
- }
- bool = false;
- condition.signal();//this.notify();
- } finally {
- lock.unlock();
- }
- }
- }
这样看来,Condition和传统的线程通信没什么区别,Condition的强大之处在于它可以为多个线程间建立不同的Condition,下面引入API中的一段代码,加以说明。
- class BoundedBuffer {
- final Lock lock = new ReentrantLock();//锁对象
- final Condition notFull = lock.newCondition();//写线程条件
- final Condition notEmpty = lock.newCondition();//读线程条件
- final Object[] items = new Object[100];//缓存队列
- int putptr/*写索引*/, takeptr/*读索引*/, count/*队列中存在的数据个数*/;
- public void put(Object x) throws InterruptedException {
- lock.lock();
- try {
- while (count == items.length)//如果队列满了
- notFull.await();//阻塞写线程
- items[putptr] = x;//赋值
- if (++putptr == items.length) putptr = 0;//如果写索引写到队列的最后一个位置了,那么置为0
- ++count;//个数++
- notEmpty.signal();//唤醒读线程
- } finally {
- lock.unlock();
- }
- }
- public Object take() throws InterruptedException {
- lock.lock();
- try {
- while (count == 0)//如果队列为空
- notEmpty.await();//阻塞读线程
- Object x = items[takeptr];//取值
- if (++takeptr == items.length) takeptr = 0;//如果读索引读到队列的最后一个位置了,那么置为0
- --count;//个数--
- notFull.signal();//唤醒写线程
- return x;
- } finally {
- lock.unlock();
- }
- }
- }
1. 一个写线程执行,调用put方法;
2. 判断count是否为100,显然没有100;
3. 继续执行,存入值;
4. 判断当前写入的索引位置++后,是否和100相等,相等将写入索引值变为0,并将count+1;
5. 仅唤醒读线程阻塞队列中的一个;
6. 一个读线程执行,调用take方法;
7. ……
8. 仅唤醒写线程阻塞队列中的一个。
这就是多个Condition的强大之处,假设缓存队列中已经存满,那么阻塞的肯定是写线程,唤醒的肯定是读线程,相反,阻塞的肯定是读线程,唤醒的肯定是写线程,那么假设只有一个Condition会有什么效果呢,缓存队列中已经存满,这个Lock不知道唤醒的是读线程还是写线程了,如果唤醒的是读线程,皆大欢喜,如果唤醒的是写线程,那么线程刚被唤醒,又被阻塞了,这时又去唤醒,这样就浪费了很多时间。
- Condition
- Condition
- Condition
- Condition
- Condition
- Condition
- Condition
- Condition
- Condition
- boost condition
- boost condition
- race condition
- Condition Codes
- condition.pl
- pthread_cond_singal condition
- condition variables
- pthread condition
- java Condition
- Qt Creator 常用快捷键
- STM32F103系列单片机学习笔记1方便以后查看
- eclipse 泛型报错
- Mysql常用引擎
- javascript 有关this的指向
- Condition
- 第四个页面 PC端
- sql 查询的结果中有NULL值,如何替换掉
- [leetcode]165. Compare Version Numbers
- JAVA内部类
- 用循环和递归处理迷宫问题
- 前端未来趋势
- 误删libc.so.6
- 反畸变