本教程操作环境:windows7系统、java10版,DELL G3电脑。
1.方法介绍
put(E e) 添加一个非空元素,同时会阻塞住,直到另一个线程调用take()
take() 取出一个元素,如果队列为空,阻塞,直到另一个线程调用put(E e)
2.入队put方法实例
publicvoidput(Ee)throwsInterruptedException{
if(e==null)thrownewNullPointerException();//元素不可为null
//三个参数分别是:传输的元素,是否需要超时,超时的时间
if(transferer.transfer(e,false,0)==null){
//如果传输失败,直接让线程中断并抛出中断异常
Thread.interrupted();
thrownewInterruptedException();
}
}
3.出队take方法实例
publicEtake()throwsInterruptedException{
Ee=transferer.transfer(null,false,0);
if(e!=null)
returne;
Thread.interrupted();
thrownewInterruptedException();
}
原文来自:https://www.py.cn
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容