转载自:
http://blog.youkuaiyun.com/mn11201117/article/details/8695880
public class NativeSynchronousQueue<E> {
boolean putting = false;
E item = null;
public synchronized E take() throws InterruptedException {
while (item == null)
wait();
E e = item;
item = null;
notifyAll();
return e;
}
public synchronized void put(E e) throws InterruptedException {
if (e==null) return;
while (putting)
wait();
putting = true;
item = e;
notifyAll();
while (item!=null)
wait();
putting = false;
notifyAll();
}
}
本文介绍了一个简单的同步队列实现方式,使用 Java 语言编写。该队列通过 synchronized 关键字来确保线程间的同步操作,并利用 wait 和 notifyAll 方法来协调 put 和 take 操作之间的等待与唤醒机制。
2371

被折叠的 条评论
为什么被折叠?



