7

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user1947236
  • 673
  • 3
  • 12
  • 27

3 Answers3

9

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 stop method, just loop all the threads and call interrupt on them:

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

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

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

}
Shivam
  • 2,134
  • 1
  • 18
  • 28
  • 4
    And let's just hope that there's no blocking IO on the thread. – nullpotent Jan 09 '13 at 21:32
  • I tried this, its not working. It have it set to print "Threads Stopped" after the for loop. It prints threads stopped but the function on those threads is still running. I also tried thread.stop() – user1947236 Jan 09 '13 at 22:00
  • @user1947236 You need to keep on checking `Thread.interrupted()' in your actual implementation of thread. Check the example I put above. – Shivam Jan 09 '13 at 22:49
3

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

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 run method 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 ExecutorService returned by the various factory methods provided by the Executors class. They would indeed handle the lifecycle of threads for you.
assylias
  • 321,522
  • 82
  • 660
  • 783
3

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

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722