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?
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.
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.
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.
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.
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);
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With