停止特定的 Java 线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7786305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stopping a specific java thread
提问by mikeyP
I have a button "addCashier" which is creating a thread called "Cashier" now this thread is just simply generating orders every 4 seconds, a while(true) loop in the run() method of the thread. All is good there, but now I want to add a button to simulate cashiers logging off. I added a boolean variable to my while loop onDuty and a public function logOff() which sets this onDuty bool to false to get me out of the run's while loop. My problem now is from my gui class how can I call a function on a specific thread? Each cashier thread has been generated at runtime so I don't know their names.
我有一个按钮“addCashier”,它正在创建一个名为“Cashier”的线程,现在这个线程只是简单地每 4 秒生成一次订单,线程的 run() 方法中的 while(true) 循环。一切都很好,但现在我想添加一个按钮来模拟收银员注销。我向我的 while 循环 onDuty 和一个公共函数 logOff() 添加了一个布尔变量,该函数将这个 onDuty bool 设置为 false 以使我退出运行的 while 循环。我现在的问题是我的 gui 类如何在特定线程上调用函数?每个收银员线程都是在运行时生成的,所以我不知道它们的名字。
I hope I made sense. Thanks in advance.
我希望我说得有道理。提前致谢。
回答by Tomasz Nurkiewicz
Thread t = CashierThread(); //keep the reference to thread somewhere...
Now instead of a boolean property use built-in interrupted flag:
现在使用内置中断标志代替布尔属性:
public void run() {
while(!Thread.currentThread().isInterrupted()) {
//...
}
}
When you want to turn of the thread by clicking on a button simply call:
当您想通过单击按钮关闭线程时,只需调用:
t.interrupt();
Of course you need to have access to t
variable from the client code.
当然,您需要t
从客户端代码访问变量。
回答by Bhaskar
You can store each thread's reference into a HashMap
- along with its id
or Name
as the key. Later when you want to deal with one particular Cashier thread , use the Name
or id
to fetch the corresponding Thread
from the HashMap and call the appropriate logOff()
method on it.
您可以将每个线程的引用HashMap
与它的id
或Name
作为键一起存储到一个- 中。稍后当您想要处理一个特定的 Cashier 线程时,使用Name
或从 HashMap 中id
获取相应Thread
的内容并logOff()
在其上调用适当的方法。
回答by Vivek Viswanathan
If collecting the reference to all the threads is a problem, One other way could be having a public static synchronized HashMap that has the threadId(random number assigned at runtime to each thread) as the key and the boolean as the value. You can modify the while loop to pick the corresponding boolean value from this centralized Map. This would let you log-off a particular cashier.
如果收集对所有线程的引用是一个问题,另一种方法可能是使用公共静态同步 HashMap,该 HashMap 将 threadId(在运行时分配给每个线程的随机数)作为键,将布尔值作为值。您可以修改 while 循环以从这个集中的 Map 中选择相应的布尔值。这将让您注销特定的收银员。
回答by PC.
You can keep a reference of the Thread object somewhere so that u can call threadObj.logOff()
您可以在某处保留 Thread 对象的引用,以便您可以调用 threadObj.logOff()
If you are not willing to do that then while creating a thred u can assign a unique name to the thread.
如果您不愿意这样做,那么在创建线程时您可以为线程分配一个唯一的名称。
public void run ()
{
this.setName(strCashireID);
......
}
At runtime, u can get the thread by:
在运行时,您可以通过以下方式获取线程:
Thread getThread(String strCashireID) {
ThreadGroup threadGroup = Thread.currentThread( ).getThreadGroup( );
Threads[] threads = new Thread[ threadGroup.activeCount() ];
threadGroup.enumerate(threads);
for (int nIndex=0; nIndex<threads.length; nIndex++) {
if(threads[nIndex] != null && threads.getName().equals(strCashireID) {
return threads[nIndex];
}
}
return null;
}
I'll still suggest that u store the thread objects in a hash map instead of enumerating them at runtime.
我仍然建议您将线程对象存储在哈希映射中,而不是在运行时枚举它们。