Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java stop thread all threads running from class

Tags:

java

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?

like image 326
user1947236 Avatar asked Jan 09 '13 21:01

user1947236


People also ask

How do I stop all threads in Java?

Whenever we want to stop a thread from running state by calling stop() method of Thread class in Java. This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected. A thread will also move to the dead state automatically when it reaches the end of its method.

How do I destroy all threads?

Modern ways to suspend/stop a thread are by using a boolean flag and Thread. interrupt() method. Using a boolean flag: We can define a boolean variable which is used for stopping/killing threads say 'exit'. Whenever we want to stop a thread, the 'exit' variable will be set to true.

How do you stop a thread from looping in Java?

1 - Since Thread. stop() is deprecated, I can implement a stopThisThread() method that uses a n atomic check-condition variable. 2 - Send a Death Event object or something like that to the queue. When the thread fetches a death event, it exits.

How do I force a thread to stop?

To end the thread before going through the current loop you can use the break keyword. This avoids using deprecated methods such as Thread. stop() . The thread will finish the current task and then stop itself naturally.


1 Answers

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

}
like image 163
Shivam Avatar answered Oct 24 '22 14:10

Shivam