
本教程操作环境:windows7系统、java10版,DELL G3电脑。
1.方法介绍
put(E e) 添加一个非空元素,同时会阻塞住,直到另一个线程调用take()
take() 取出一个元素,如果队列为空,阻塞,直到另一个线程调用put(E e)
2.入队put方法实例
1 2 3 4 5 6 7 8 9 | public void put(E e) throws InterruptedException {
if (e == null ) throw new NullPointerException();
if (transferer.transfer(e, false , 0) == null ) {
Thread.interrupted();
throw new InterruptedException();
}
}
|
3.出队take方法实例
1 2 3 4 5 6 7 | public E take() throws InterruptedException {
E e = transferer.transfer( null , false , 0);
if (e != null )
return e;
Thread.interrupted();
throw new InterruptedException();
}
|
以上就是SynchronousQueue在java中的元素增减方法,在理解了SynchronousQueue的用法后,我们就可以对元素的增加或删除有所变动,学会后赶快试试代码的运行吧。