Reputation: 2244
class Thread3_1 extends Thread {
public static int count = 0;
String tname1;
public Thread3_1(String threadname) {
tname1 = threadname;
}
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(tname1 + " : " + i);
count++;
if (count == 2) {
try {
sleep(1000);
count = 0;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (isInterrupted()) {
System.out.println("Stop Thread");
return;
}
}
}
class Thread3_2 extends Thread {
String tname2;
public Thread3_2(String threadname) {
tname2 = threadname;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(tname2 + " : " + i);
if (i == 5) {
Thread.currentThread().interrupt();
System.out.println("Going to interrupt Thread1");
}
}
}
}
Thread is executing after giving interrupt()
Upvotes: 2
Views: 238
Reputation: 9721
From the Java Tutorials:
Many methods that throw InterruptedException, such as sleep, are designed to cancel their current operation and return immediately when an interrupt is received.
What if a thread goes a long time without invoking a method that throws InterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received.*
*In other words: Other methods that do not throw InterruptedException must periodically invoke Thread.interrupted, which returns true if an interrupt has been received.
Explained here: http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
Upvotes: 0
Reputation: 7692
As noted above you need to interrupt Thread3_1 and not Thread3_2. After applying this fix you will still have one issue left:
try {
sleep(1000);
count = 0;
} catch (InterruptedException e) {
e.printStackTrace();
}
So if at sleep interrupt happens, interrupt flag will be cleared and isInterrupted() method will return false, so below condition will not be met:
if (isInterrupted()) {
System.out.println("Stop Thread");
return;
}
So you need to handle this in catch block as well, i.e. return if in catch (got interrupted)
Upvotes: 0
Reputation: 22456
The thread you are interrupting must be designed to deal with the interruption request, by exiting gracefully from the run() method. That means making sure that InterruptedExceptions cause the code to break out of loops. It also means checking the interrupted flag periodically inside loops. There are other scenarios which are harder to deal with, such as non-interruptible IO operations.
It is also a good practice that the target thread re-marks itself as interrupted, so that the callers up in the call stack also get an indication of the interruption (Use Thread.currentThread().interrupt() in catch statements).
Upvotes: 0
Reputation: 692121
Interrupting a thread just sets an interrupt flag to true
for this thread.
It's the responsibility of the thread itself to regurlarly check the value of this flag and stop executing ASAP (by returning from the run()
method) as soon as it's true.
When the interrupt flag is set while the thread is sleeping (or when it's blocked inside a bocking call like wait()
, await()
, etc.), an InterruptedException is thrown by the blocking method. It's also the responsibility of the thread to cacth this exception and stop executing ASAP.
Your first thread regularly checks the flag and exits when it's true, but fails to do so when an InterruptedException is caught.
Your second thread interrupts itself, but doesn't return from its run()
method when it does so.
Upvotes: 3
Reputation: 51473
You interrupt Thread3_2 and not Thread3_1.
In the run method of Thread3_2 you are calling
Thread.currentThread().interrupt();
This will send an interrupt to the current executing thread and this is an instance of Thread3_2. If you want to interrupt Thread3_1 you need a reference to that thread.
For example:
class Thread3_2 extends Thread{
Thread threadToInterrupt;
public Thread3_2(Thread threadToInterrupt) {
this.threadToInterrupt = threadToInterrupt;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(tname2+ " : " +i);
if(i == 5){
threadToInterrupt.interrupt();
System.out.println("Going to interrupt Thread1");
}
}
}
}
Upvotes: 1