停止特定的java线程

发布于 2024-12-10 18:23:13 字数 1434 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里人 2024-12-17 18:23:13
Thread t = CashierThread();  //keep the reference to thread somewhere...

现在,使用内置中断标志而不是布尔属性:

public void run() {
  while(!Thread.currentThread().isInterrupted()) {
    //...
  }
}

当您想通过单击按钮来关闭线程时,只需调用:

t.interrupt();

当然您需要从客户端代码访问 t 变量。

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.

嗳卜坏 2024-12-17 18:23:13

您可以将每个线程的引用及其 idName 作为键存储到 HashMap 中。稍后,当您想要处理某个特定的 Cashier 线程时,请使用 Nameid 从 HashMap 中获取相应的 Thread 并调用相应的其上的 logOff() 方法。

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.

童话 2024-12-17 18:23:13

如果收集对所有线程的引用是一个问题,另一种方法可能是使用一个公共静态同步 HashMap,该 HashMap 将 threadId(在运行时分配给每个线程的随机数)作为键,将布尔值作为值。您可以修改 while 循环以从此集中式 Map 中选取相应的布尔值。这可以让您注销特定的收银员。

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.

玉环 2024-12-17 18:23:13

您可以在某处保留 Thread 对象的引用,以便您可以调用 threadObj.logOff()

如果您不愿意这样做,那么在创建线程时您可以为线程分配一个唯一的名称。

public void run ()
{
    this.setName(strCashireID);
    ......
}

在运行时,您可以通过以下方式获取线程:

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;
}

我仍然建议您将线程对象存储在哈希映射中,而不是在运行时枚举它们。

You can keep a reference of the Thread object somewhere so that u can call 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文