多线程之线程通信条件Condition
Condition是Locks锁下的还有一种线程通信之间唤醒、堵塞的实现。它以下的await,和signal可以实现Object下的wait,notify和notifyAll的所有功能,除此之外改监视器和已绑定到每一个条件,可以实现多条件的监听。Condition实质是被绑定到一个锁上,腰围特定的Lock实例获得Condition,即用 newCondition()方法。
Condition下的await()相对于Object下的wait(); 堵塞或中断之前状况,让其处于等待状态。
Condition下的Signal();相当于Object下的notify();唤醒一个等待的状态。
Condition应用:
假设有一个缓存区,大小100,它支持put和take方法。假设试图在空的缓存区上运行take操作,则在某一项变得可用之前,线程一直堵塞;假设试图在满的缓存区上运行操作,则在有空间变得可用之前,线程一直讲堵塞。我们能够使用两个条件,一个记录缓存区的满,一个记录缓存区的空。
package andy.thread.test; import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; /**
* @author Zhang,Tianyou
* @version 2014年11月9日 上午11:39:30
*/ public class ThreadConditiionBoundedBuffer { public static void main(String[] args) { BoundedBuffer buffer = new ThreadConditiionBoundedBuffer().new BoundedBuffer(); for (int i = 0; i < 100; i++) {
int task = i;
new Thread((new Runnable() {
public void run() {
try {
String putString = Thread.currentThread().getName() + task;
buffer.put(putString);
System.out.println(putString);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
})).start();
} for (int i = 0; i < 100; i++) {
new Thread((new Runnable() {
public void run() {
try {
System.out.println( Thread.currentThread() + "take " + buffer.take());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
})).start();
}
} class BoundedBuffer {
// 定义锁
Lock lock = new ReentrantLock();
Condition notFull = lock.newCondition(); // 没有满
Condition notEmpty = lock.newCondition(); // 不为空 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;
++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;
--count;
notFull.signal();
return x; } finally {
lock.unlock();
}
}
} }
运行效果例如以下:
Thread-00
Thread-22
Thread-11
Thread-1414
Thread-1212
Thread-1010
Thread-88
Thread-66
Thread-44
Thread-1616
Thread-1818
Thread-77
Thread-2222
Thread-2020
Thread-55
Thread-33
Thread-2424
Thread-2626
Thread-99
Thread-2828
Thread-1313
Thread-3030
Thread-1717
Thread-1111
Thread-3232
Thread-1919
Thread-1515
Thread-2121
Thread-2323
Thread-2525
Thread-3434
Thread-3131
Thread-2727
Thread-3333
Thread-2929
Thread-3535
Thread-3636
Thread-3737
Thread-3838
Thread-4040
Thread-3939
Thread-4242
Thread-6464
Thread-6666
Thread-6262
Thread-6060
Thread-5858
Thread-4747
Thread-5656
Thread-4545
Thread-5050
Thread-4848
Thread-5252
Thread-4646
Thread-5454
Thread-4444
Thread-4343
Thread-6868
Thread-4141
Thread-7070
Thread-7272
Thread-7474
Thread-7676
Thread-4949
Thread-7878
Thread-6969
Thread-8080
Thread-7171
Thread-8282
Thread-7373
Thread-7575
Thread-8484
Thread-8686
Thread-7777
Thread-8888
Thread-8181
Thread-9090
Thread-8989
Thread-8383
Thread-7979
Thread-8787
Thread-8585
Thread-6767
Thread-9292
Thread-9191
Thread-6565
Thread-9494
Thread-6363
Thread-6161
Thread-9696
Thread-9898
Thread-5959
Thread-5757
Thread-5555
Thread-5353
Thread-9393
Thread-9595
Thread[Thread-100,5,main]take Thread-00
Thread-9999
Thread[Thread-101,5,main]take Thread-22
Thread[Thread-102,5,main]take Thread-11
Thread-9797
Thread[Thread-104,5,main]take Thread-44
Thread[Thread-106,5,main]take Thread-66
Thread[Thread-108,5,main]take Thread-88
Thread[Thread-110,5,main]take Thread-1010
Thread-5151
Thread[Thread-112,5,main]take Thread-1212
Thread[Thread-114,5,main]take Thread-1616
Thread[Thread-116,5,main]take Thread-1818
Thread[Thread-118,5,main]take Thread-77
Thread[Thread-120,5,main]take Thread-2020
Thread[Thread-122,5,main]take Thread-2222
Thread[Thread-124,5,main]take Thread-55
Thread[Thread-103,5,main]take Thread-1414
Thread[Thread-126,5,main]take Thread-33
Thread[Thread-128,5,main]take Thread-2424
Thread[Thread-105,5,main]take Thread-2626
Thread[Thread-130,5,main]take Thread-99
Thread[Thread-107,5,main]take Thread-2828
Thread[Thread-132,5,main]take Thread-1313
Thread[Thread-109,5,main]take Thread-3030
Thread[Thread-134,5,main]take Thread-1515
Thread[Thread-111,5,main]take Thread-1111
Thread[Thread-136,5,main]take Thread-1717
Thread[Thread-113,5,main]take Thread-3232
Thread[Thread-138,5,main]take Thread-1919
Thread[Thread-140,5,main]take Thread-2121
Thread[Thread-115,5,main]take Thread-2323
Thread[Thread-142,5,main]take Thread-2525
Thread[Thread-117,5,main]take Thread-3434
Thread[Thread-144,5,main]take Thread-3131
Thread[Thread-119,5,main]take Thread-2727
Thread[Thread-146,5,main]take Thread-2929
Thread[Thread-148,5,main]take Thread-3333
Thread[Thread-121,5,main]take Thread-3535
Thread[Thread-150,5,main]take Thread-3636
Thread[Thread-152,5,main]take Thread-3737
Thread[Thread-123,5,main]take Thread-4040
Thread[Thread-125,5,main]take Thread-3838
Thread[Thread-127,5,main]take Thread-3939
Thread[Thread-154,5,main]take Thread-4242
Thread[Thread-129,5,main]take Thread-6666
Thread[Thread-156,5,main]take Thread-6464
Thread[Thread-131,5,main]take Thread-6262
Thread[Thread-158,5,main]take Thread-6060
Thread[Thread-160,5,main]take Thread-5656
Thread[Thread-137,5,main]take Thread-4545
Thread[Thread-133,5,main]take Thread-5858
Thread[Thread-135,5,main]take Thread-4747
Thread[Thread-162,5,main]take Thread-5454
Thread[Thread-139,5,main]take Thread-5252
Thread[Thread-164,5,main]take Thread-5050
Thread[Thread-143,5,main]take Thread-4646
Thread[Thread-141,5,main]take Thread-4848
Thread[Thread-145,5,main]take Thread-4444
Thread[Thread-166,5,main]take Thread-4343
Thread[Thread-147,5,main]take Thread-6868
Thread[Thread-168,5,main]take Thread-4141
Thread[Thread-157,5,main]take Thread-7070
Thread[Thread-151,5,main]take Thread-7272
Thread[Thread-170,5,main]take Thread-7474
Thread[Thread-155,5,main]take Thread-7676
Thread[Thread-153,5,main]take Thread-4949
Thread[Thread-172,5,main]take Thread-7878
Thread[Thread-149,5,main]take Thread-6969
Thread[Thread-174,5,main]take Thread-8080
Thread[Thread-159,5,main]take Thread-7171
Thread[Thread-176,5,main]take Thread-8282
Thread[Thread-161,5,main]take Thread-7373
Thread[Thread-163,5,main]take Thread-7575
Thread[Thread-178,5,main]take Thread-8484
Thread[Thread-165,5,main]take Thread-8686
Thread[Thread-167,5,main]take Thread-7777
Thread[Thread-180,5,main]take Thread-8888
Thread[Thread-182,5,main]take Thread-8181
Thread[Thread-169,5,main]take Thread-9090
Thread[Thread-184,5,main]take Thread-8989
Thread[Thread-171,5,main]take Thread-8383
Thread[Thread-173,5,main]take Thread-7979
Thread[Thread-189,5,main]take Thread-8585
Thread[Thread-183,5,main]take Thread-6161
Thread[Thread-192,5,main]take Thread-5959
Thread[Thread-187,5,main]take Thread-9898
Thread[Thread-185,5,main]take Thread-9696
Thread[Thread-179,5,main]take Thread-6363
Thread[Thread-194,5,main]take Thread-5757
Thread[Thread-181,5,main]take Thread-9494
Thread[Thread-190,5,main]take Thread-6565
Thread[Thread-177,5,main]take Thread-9191
Thread[Thread-196,5,main]take Thread-5555
Thread[Thread-175,5,main]take Thread-9292
Thread[Thread-188,5,main]take Thread-6767
Thread[Thread-191,5,main]take Thread-5353
Thread[Thread-198,5,main]take Thread-9393
Thread[Thread-186,5,main]take Thread-8787
Thread[Thread-193,5,main]take Thread-9595
Thread[Thread-195,5,main]take Thread-9999
Thread[Thread-197,5,main]take Thread-9797
Thread[Thread-199,5,main]take Thread-5151
多线程之线程通信条件Condition的更多相关文章
- 多线程之线程通信条件Condition二
接上一篇,实现Condition三个条件,有这样一个应用: 1. 有三个进程,第一个进程运行1次,第二个进程运行2次,第三个进程运行3次: 2. 先运行第二个进程,然后第一个,然后第三个: 3. 依 ...
- Java之多线程开发时多条件Condition接口的使用
转:http://blog.csdn.net/a352193394/article/details/39454157 我们在多线程开发中,可能会出现这种情况.就是一个线程需要另外一个线程满足某某条件才 ...
- java 多线程:线程通信-等待通知机制wait和notify方法;(同步代码块synchronized和while循环相互嵌套的差异);管道通信:PipedInputStream;PipedOutputStream;PipedWriter; PipedReader
1.等待通知机制: 等待通知机制的原理和厨师与服务员的关系很相似: 1,厨师做完一道菜的时间不确定,所以厨师将菜品放到"菜品传递台"上的时间不确定 2,服务员什么时候可以取到菜,必 ...
- Scala学习笔记之Actor多线程与线程通信的简单例子
题目:通过子线程读取每个文件,并统计单词数,将单词数返回给主线程相加得出总单词数 package review import scala.actors.{Actor, Future} import s ...
- java 多线程 day04 线程通信
package com.czbk.thread; /** * Created by chengtao on 17/12/3. * 需求: 子线程先运行10次,然后主线程运行 100次,依次运行50次 ...
- java多线程(线程通信-等待换新机制-代码优化)
等待唤醒机制涉及方法: wait():让线程处于冻结状态,被wait的线程会被存储到线程池中. noticfy():唤醒同一个线程池中一个线程(任意也可能是当前wait的线程) notifyAll() ...
- 6.显示锁Lock 和 线程通信Condition
显示锁 Lock 一.用于解决多线程 安全问题的方式: synchronized: 1.同步代码块 2.同步方法 jdk1.5 后:第三种:同步锁Lock (注意:同步(synchro ...
- 0038 Java学习笔记-多线程-传统线程间通信、Condition、阻塞队列、《疯狂Java讲义 第三版》进程间通信示例代码存在的一个问题
调用同步锁的wait().notify().notifyAll()进行线程通信 看这个经典的存取款问题,要求两个线程存款,两个线程取款,账户里有余额的时候只能取款,没余额的时候只能存款,存取款金额相同 ...
- 06_Java多线程、线程间通信
1. 线程的概念 1.1多进程与多线程 进程:一个正在执行的程序.每个进程执行都有一个执行顺序,该顺序是一个执行路径,或叫一个控制单元. 一个进程至少有一个线程. 线程:就是进程中的一个独立 ...
随机推荐
- 基于Visual C++2013拆解世界五百强面试题--题12-进制转换
编程实现,把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数. 转换成二进制,直接循环移位依次取每一位,判断1或0然后将相应字符放入字符串缓冲区中. 对于十六进制, ...
- Bitmap recycle()
Bitmap调用recycle? When? Bitmap有一个recycle方法,意思非常easy,回收Bitmap的空间. Q 1: Bitmap是否有调用recycle方法的必要性? A: 嵌入 ...
- URlRewriter组件下载及使用说明
下载地址:http://www.chx99.cn/file/URLRewriter.rar在web.config添加如下配置节: <httpHandlers> <add ver ...
- Android滑动事件冲突
首先,我们假设这样一个场景:一个ViewPager里面嵌套一个ViewPager,内部滑动方向和外部滑动方向一样时,该怎么解决这一冲突呢? 针对滑动冲突这里给出两种解决方案:外部拦截法,内部拦截法. ...
- C#关键字列表
- SQL 插入语句汇总
INSERT VALUES 插入一行或多行到目标表中 -- single row INSERT INTO Sales.MyOrders(custid, empid, orderdate, shipco ...
- 「OC」@property @synthesize和id
一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...
- clear伪类使用
都知道float会脱离文档流 用什么办法撑开父元素呢? 手动在本区块的所有float元素之后加上一个块元素并对其添加clear:both 可以 但是这样还要再去修改html页面 而且多了一个仅仅是 ...
- 转: seajs手册与文档之 -- require规则
require 规则 正确拼写 不要修改 使用直接量 动态依赖的小提示 书写规则 使用 SeaJS 书写模块代码时,需要遵循一些简单规则: 1. 正确拼写 在模块代码中,第一个参数 必须 命名为 re ...
- 转:FileReader详解与实例---读取并显示图像文件
~~~针对需要读取本地图像,并立即显示在浏览器的情况,由于chrome firefox出于安全限制,input file并不返回文件的真实路径,经测试IE6/7/8都会返回真实路径,所以chrome, ...