您的位置:首页 > 其它

阻塞算法实现synchronized实现方式

2013-08-27 00:12 155 查看
转载自:http://blog.csdn.net/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();	    }}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: