Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does calling Thread.interrupt() before a Thread.join() cause the join() to throw an InterruptedException immediately?

Basically, what the question title says.

Thread t = new Thread(someRunnable);
t.start();
t.interrupt();
t.join(); //does an InterruptedException get thrown immediately here?

From my own tests, it seems to, but just wanted to be sure. I'm guessing Thread.join() checks the interrupted status of the thread before doing its "wait" routine?

like image 958
XåpplI'-I0llwlg'I - Avatar asked May 17 '12 08:05

XåpplI'-I0llwlg'I -


People also ask

What happens when we call the interrupt method on a thread?

In Java Threads, if any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException.

What does thread currentThread () interrupt () do?

By calling Thread. currentThread(). interrupt() , you set the interrupt flag of the thread, so higher-level interrupt handlers will notice it and can handle it appropriately.

What is the use of the interrupt () function of thread class?

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.

What causes Java InterruptedException?

Class InterruptedException. Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception.


2 Answers

Does calling Thread.interrupt() before a Thread.join() cause the join() to throw an InterruptedException immediately?

No it will not throw. Only if the current thread that is calling the join() method gets interrupted will join() throw InterruptedException. t.interrupt() is interrupting the thread you just started, while t.join() will only throw InterruptedException if the thread that is doing the join-ing (maybe the main thread?) is itself interrupted.

 Thread t = new Thread(someRunnable);
 t.start();
 t.interrupt();
 t.join();  // will _not_ throw unless this thread calling join gets interrupted

Also it is important to realize that interrupting a thread does not cancel it and join() is not like a Future in that it will return the exception the thread threw.

When you interrupt a thread, any calls the thread is making to sleep(), wait(), join(), and other interruptible methods will throw InterruptedException. If those methods are not called then the thread will continue running. If a thread does throw a InterruptedException in response to being interrupted and then quits, that exception will be lost unless you you used t.setDefaultUncaughtExceptionHandler(handler).

In your case, if the thread is interrupted and finishes because it returns, then the join will finish -- it will not throw an exception. Common thread code to handle an interrupt properly is as follows:

 public void run() {
    try {
       Thread.sleep(10000);
    } catch (InterruptedException e) {
       // a good pattern is to re-interrupt the thread when you catch
       Thread.currentThread().interrupt();
       // another good pattern is to make sure that if you _are_ interrupted,
       // that you stop the thread
       return;
    }
 }
like image 136
Gray Avatar answered Sep 17 '22 21:09

Gray


interrupt() interrupts the thread you interrupted, not the thread doing the interrupting.

c.f.

Thread.currentThread().interrupt();
t.join(); // will throw InterruptedException 
like image 35
Peter Lawrey Avatar answered Sep 17 '22 21:09

Peter Lawrey