[英]How to make a BufferedReader yield?
Let's say I've got two threads running. 假设我有两个线程正在运行。 Thread #1 reads System.in like this: 线程1像这样读取System.in:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while (true) {
s=br.readLine();
if(s!=null)
doSomethingWithThat(s);
}
}
I also have Thread #2
that performs some operations, among others needs to read System.in
when certain things happens (that in general are not triggered by Thread #1
.) 我还具有执行某些操作的Thread #2
,其中一些操作需要在某些情况下(通常不是由Thread #1
触发)读取System.in
。
Is it possible to somehow interrupt Thread #1
so that br.readLine()
would return null
and #Thread #2
could read System.in
? 是否有可能以某种方式中断Thread #1
以便br.readLine()
return null
, #Thread #2
可以读取System.in
呢?
Added this before readLine() & did the trick 在readLine()之前添加了此功能并完成了技巧
while(System.in.available()<=0 || yield)
Sleep.millis(420);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.