Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interrupt a sleeping thread

Trying to interrupt a running thread, in this example, t1, which is executed by a thread in a thread pool.

t2 is the one that sends the interrupt.

I'm unable to stop the running t1, t1 does not get InterruptedException.

What am I missing?

    Executor exec1 = Executors.newFixedThreadPool(1);

    // task to be interrupted
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                System.out.println("starting uninterruptible task 1");
                Thread.sleep(4000);
                System.out.println("stopping uninterruptible task 1");
            } catch (InterruptedException e) {
                assertFalse("This line should never be reached.", true);
                e.printStackTrace();
            }               
        }           
    };
    final Thread t1 = new Thread(runnable);


    // task to send interrupt
    Runnable runnable2 = new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                t1.interrupt();
                System.out.println("task 2 - Trying to stop task 1");
                Thread.sleep(5000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }               
        }           
    };
    Thread t2 = new Thread(runnable2);

    exec1.execute(t1);
            t2.start();
    t2.join();
like image 784
Lydon Ch Avatar asked Aug 22 '11 08:08

Lydon Ch


People also ask

How do I cancel thread sleep?

Thread. Sleep cannot be cancelled. To inject a pause, consider using the Cancellation Token instead.

How a thread can be interrupted?

A thread can send an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. This means interruption of a thread is caused by any other thread calling the interrupt() method. void interrupt() - Interrupts the thread.

What does thread currentThread () interrupt () do?

currentThread(). isInterrupted() tests whether this thread has been interrupted without affecting the interrupted status. Now, let's get back to our case. So, we know that the thread was interrupted since we caught InterruptedException , but the interrupted status was cleared by Thread.

What happens when a thread is interrupted?

Threads can be interrupted, and when a thread is interrupted, it will throw InterruptedException. In the next sections, we'll see InterruptedException in detail and learn how to respond to it.


1 Answers

Seems like you misunderstand threads and Executors. You create two threads object for two runnables, but start only one of them (t2), t1 you pass to Executor to run inside it. But executor does not need Thread to be supplied -- it just need Runnable implementation. Executor itself is a thread pool (usually, but it's not required), and it just creates (and pool) threads inside it. It sees you thread just as simple Runnable (which is Thread implements). So you actualy send interrupt to the thread which was never started.

If you really want to make your code works, you should remove Executor, and just start both threads explicitly.

like image 191
BegemoT Avatar answered Oct 03 '22 04:10

BegemoT