java Java停止线程从类运行的所有线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14246841/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:36:23  来源:igfitidea点击:

Java stop thread all threads running from class

java

提问by user1947236

what I need to do is be able to stop all threads running from one thread class that implements runnable. This is what I mean: here is the beginning of my "thread" class:

我需要做的是能够停止从一个实现可运行的线程类运行的所有线程。这就是我的意思:这是我的“线程”类的开头:

public class HTTP extends Thread
{   
    int threadNumber;
    String host;
    int port;
    int timeLeft;
    private BufferedReader LocalBufferedReader;

    public HTTP(int threadNumber, String host, int port, int timeLeft)
    {
        this.threadNumber = threadNumber;
        this.host= host;
        this.port = port;
        this.timeLeft = (timeLeft * 1000);
    }

  public void run()
  {

This is how I am creating the multiple threads to do this:

这就是我创建多个线程来执行此操作的方式:

 for (int n = 1; n <= m; n++) {
      new HTTP(n + 1, str, j, k).start();
    }

m is the number of threads to create. This can be anywhere from 50-1000. Now what I need to do is just abruptly stop all of them at once. How can I do that?

m 是要创建的线程数。这可以是 50-1000 之间的任何地方。现在我需要做的就是突然停止所有这些。我怎样才能做到这一点?

回答by Shivam

First store all the threads:

首先存储所有线程:

ArrayList<Thread> threads = new ArrayList<Thread>();
for (int n = 1; n <= m; n++) {
    Thread t = new HTTP(n + 1, str, j, k);
    threads.add(t);
    t.start();
 }

Now for stopmethod, just loop all the threads and call interrupt on them:

现在对于stop方法,只需循环所有线程并对其调用中断:

for(Thread thread : threads)
{
    thread.interrupt();
}

Make sure to check isIntruppted()in your HTTP threads. So you would do something like this:

确保签isIntruppted()入您的 HTTP 线程。所以你会做这样的事情:

public class InterruptTest {

    static class TThread extends Thread {
        public void run() {
            while(!isInterrupted()) {
                System.out.println("Do Work!!!");
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t = new TThread();
        t.start();

        Thread.sleep(4000);
        System.out.println("Sending interrupt!!");
        t.interrupt();
        Thread.sleep(4000);
    }

}

回答by assylias

Stopping threads in Java is a cooperative process implemented with interruptions. You could store your threads and interrupt them one by one:

在 Java 中停止线程是一个通过中断实现的协作过程。您可以存储线程并一一中断它们:

List<Thread> threads = new ArrayList<> ();

for (int n = 1; n <= m; n++) {
  Thread t = new HTTP(n + 1, str, j, k);
  threads.add(t);
  t.start();
}

//later on

for (Thread t : threads) {
    t.interrupt();
}

However, it is worth noting a few things:

但是,值得注意的是以下几点:

  • this will only work if your runmethod reacts to interruption by stopping what it is doing
  • you could do the same thing more easily with a thread pool, for example by using one of the ExecutorServicereturned by the various factory methods provided by the Executorsclass. They would indeed handle the lifecycle of threads for you.
  • 只有当您的run方法通过停止正在执行的操作来对中断做出反应时,这才有效
  • 你可以通过使用一个做同样的事情也更容易与线程池,例如ExecutorService的由提供的各种工厂方法返回执行人类。他们确实会为您处理线程的生命周期。

回答by Bohemian

Firstly, starting 1000 threads is practically pointless as few of them will be scheduled to actually run concurrently.

首先,启动 1000 个线程实际上是没有意义的,因为其中很少有人会被调度为实际并发运行。

Secondly, you can't "stop" threads. All you can do is ask them nicely via cooperative code to stop.

其次,你不能“停止”线程。您所能做的就是通过合作代码很好地要求他们停止。

The easiest way to do what you want is to shutdown the JVM.

做你想做的最简单的方法是关闭JVM。