阻塞算法实现synchronized实现方式

转载自: 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();
	    }
}



   

你可能感兴趣的:(阻塞算法实现synchronized实现方式)