user1947236
user1947236

Reputation: 673

Java stop thread all threads running from class

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?

Upvotes: 7

Views: 18656

Answers (3)

Shivam
Shivam

Reputation: 2132

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

}

Upvotes: 9

Bohemian
Bohemian

Reputation: 425208

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.

Upvotes: 3

assylias
assylias

Reputation: 328735

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.

Upvotes: 3

Related Questions