您的位置:首页 > 其它

阻塞算法Semaphore实现

2013-03-20 12:55 169 查看
public class SemaphoreSynchronousQueue<E> {
E item = null;
Semaphore sync = new Semaphore(0);
Semaphore send = new Semaphore(1);
Semaphore recv = new Semaphore(0);

public E take() throws InterruptedException {
recv.acquire();
E x = item;
sync.release();
send.release();
return x;
}

public void put (E x) throws InterruptedException{
send.acquire();
item = x;
recv.release();
sync.acquire();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: