Exchanger
Exchanger 类方便了两个共同操作线程之间的双向交换;
Exchanger 通常用于一个线程填充缓冲,而另一个线程清空缓冲的情况。当两个线程在屏障处集合时(交换点),它们交换缓冲,为了解释Exchanger的使用方法,先看看其实现原理,在Exchanger中最核心的方法是exchange, 其JDK的实现是:
public V exchange(V x) throws InterruptedException {
if (!Thread.interrupted()) {
Object v = doExchange(x == null? NULL_ITEM : x, false, 0);
if (v == NULL_ITEM)
return null;
if (v != CANCEL)
return (V)v;
Thread.interrupted(); // Clear interrupt status on IE throw
}
throw new InterruptedException();
}
调用了doExchange子方法,我们来看看其具体的实现:
private Object doExchange(Object item, boolean timed, long nanos) {
Node me = new Node(item); // Create in case occupying
int index = hashIndex(); // Index of current slot
int fails = 0; // Number of CAS failures
for (;;) {
Object y; // Contents of current slot
Slot slot = arena[index];
if (slot == null) // Lazily initialize slots
createSlot(index); // Continue loop to reread
else if ((y = slot.get()) != null && // Try to fulfill
slot.compareAndSet(y, null)) {
Node you = (Node)y; // Transfer item
if (you.compareAndSet(null, item)) {
LockSupport.unpark(you.waiter);
return you.item;
} // Else cancelled; continue
}
else if (y == null && // Try to occupy
slot.compareAndSet(null, me)) {
if (index == 0) // Blocking wait for slot 0
return timed? awaitNanos(me, slot, nanos): await(me, slot);
Object v = spinWait(me, slot); // Spin wait for non-0
if (v != CANCEL)
return v;
me = new Node(item); // Throw away cancelled node
int m = max.get();
if (m > (index >>>= 1)) // Decrease index
max.compareAndSet(m, m - 1); // Maybe shrink table
}
else if (++fails > 1) { // Allow 2 fails on 1st slot
int m = max.get();
if (fails > 3 && m < FULL && max.compareAndSet(m, m + 1))
index = m + 1; // Grow on 3rd failed slot
else if (--index < 0)
index = m; // Circularly traverse
}
}
}
Slot类是从AtomicRefrence继承而来的,也就是说交换是基于原子操作的,另外从上可以看到是如何交换数据的!并且我试图补齐在其实现Class上的注释中的例子:
class FillAndEmpty {
Exchanger<DataBuffer> exchanger = new Exchanger<DataBuffer>();
DataBuffer initialEmptyBuffer = new DataBuffer();
DataBuffer initialFullBuffer = new DataBuffer();
class FillingLoop implements Runnable {
public void run() {
DataBuffer currentBuffer = initialEmptyBuffer;
try {
while (currentBuffer != null) {
addToBuffer(currentBuffer);
if (currentBuffer.full())
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex) { }}}
class EmptyingLoop implements Runnable {
public void run() {
DataBuffer currentBuffer = initialFullBuffer;
try {
while (currentBuffer != null) {
takeFromBuffer(currentBuffer);
if (currentBuffer.empty())
currentBuffer = exchanger.exchange(currentBuffer);
}
}
catch (InterruptedException ex) { }}
void start() {
new Thread(new FillingLoop()).start();
new Thread(new EmptyingLoop()).start();
}
}
分享到:
相关推荐
Vue 3.0+Vite 2.0+Vue-Router4.0+Element-Plus+Echarts 5.0+Axios 开发的后台管理系统 Vue 3.0+Vite 2.0+Vue-Router4.0+Element-Plus+Echarts 5.0+Axios 开发的后台管理系统 Vue 3.0+Vite 2.0+Vue-Router4.0+...
Android5.0+ Camera Preview based-Gralloc with ION 自己生产的PDF文件
Redis-5.0.14.1+Another-Redis-Desktop-Manager.1.5.8 windows版
dotnet-sdk-5.0.202-win-x64.exe
ibm-java2-x86_64-jre-5.0-13.1.x86_64.rpm
达内JAVA TTS5.0 PDF----ORACLE编程
JDK1.6.0_07+Tomcat5.0+Myeclipse-8.5.0-搭建开发环境
SSH整合jar包-spring5.0+hibernate5.1+struts2.5 使用测试通过的
dotnet SDK dotnet-sdk-5.0.400-linux-x64.tar.gz
1.达内JAVA TTS5.0 PDF----JAVA SE核心2 2.达内原版PDF
1.达内JAVA TTS5.0 PDF----JAVA面向对象 2.达内原版PDF
达内JAVA TTS5.0 PDF----HTML--CCS--JS
达内JAVA TTS5.0 PDF----Servlet
1.达内JAVA TTS5.0 PDF----JAVA语言基础 2.达内原版PDF
1.达内JAVA TTS5.0 PDF----JAVA SE核心1 2.达内原版PDF
达内JAVA TTS5.0 PDF----JDBC和XML
这个是安装说明文件,里面要用的安装文件,除了MYSQL 5。0太大,不能上传,其他的都上传了!!安装文件免费共享了,想快速安装成功的兄弟,请下载这个说明文件,我要收3分哦!我也想去下别人的好东西!...
ambari-2.7.5 编译过程中四个大包下载很慢,所以需要提前下载,包含:hbase-2.0.2.3.1.4.0-315-bin.tar.gz ,hadoop-3.1.1.3.1.4.0-315.tar.gz , grafana-6.4.2.linux-amd64.tar.gz ,phoenix-5.0.0.3.1.4.0-315....
elasticsearch5.0+-head谷歌插件,不需要再使用安装nodejs等等,直接使用谷歌浏览器插件即可。
这是在VDSP5.0++下面编译成功的uboot,支持的命令 base - print or set address offset bootm - boot application image from memory cmp - memory compare cp - memory copy crc32 - checksum calculation erase -...