[英]How to signal a thread in java?
我有一个正在运行的进程,它创建一个线程 object 并运行它。 现在基于 Process 中的一些事件,我希望 Thread 做出相应的反应。 更准确地说,如果 commonVar 变为真,那么线程 B 应该在 while 循环中暂停。 然后当 commonVar 变为 false 时,线程 B 应该从 run() 方法的开头开始。
请注意,线程 B 不会写入 commonVar。 它只是读取它。
public class B implements runnable
{
Boolean commanVar;
public B(boolean commanVar) {
this.commonVar = commonVar;
}
public void run()
{
while(true) {
// do some processing
}
}
}
public class A
{
public static void main(String[] args) {
Boolean commonVar = false;
Thread threadB = new Thread(new ClassB(commonVar));
threadB.start();
// some processing will happen and because of that commonVar will change.
}
}
不要停止线程。 相反,发出任务(通过 BlockingQueue)或信号(通过 Semaphore),让它们在输入时运行,并在输入耗尽时挂起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.