1. How a Thread can interrupt another Thread
1. A Thread can interrupt a sleeping thread or waiting thread by using Interrupt() of Thread class
a. Public void interrupt()
2. Interrupt() will interrupt the thread which is in sleep, waiting state.
public class MyThread extends Thread {
public void run() {
for(int i=0; i<3; i++){
try{
System.out.println(“i am lazy thread”);
Thread.sleep(2000);
}catch(Exception e){
System.out.println(“i got interrupted”);
}
}
}
public static void main(String[] args)throws Exception{
MyThread thread = new MyThread();
thread.start();
thread.interrupt();
System.out.println(“end of main thread”);
}
}
O/P :
i am lazy thread
|
i got interrupted
|
end of main thread
|
3. In above program, main thread interrupts child thread.
4. Whenever we are calling interrupt(), if the target thread not in sleeping or waiting state then there is no impact of interrupt call immediately, but it not wasted the call.
5. Interrupt call will be waited until target thread entered into sleeping or waiting state.
6. If target thread entered into sleeping or waiting state then immediately interrupt call will interrupt target thread.
7. Once sleeping or waiting thread is interrupted it will throw InterruptedException.
8. If the target thread never entered into sleeping or waiting state in it life time then no impact of interrupt call, in this case only interrupt call will be wasted.
public class MyThread extends Thread {
public void run() {
for(int i=0; i<10000; i++){
System.out.println(“i am lazy thread”);
}
try{
Thread.sleep(2000);
}catch(Exception e){
System.out.println(“i got interrupted”);
}
}
public static void main(String[] args)throws Exception{
MyThread thread = new MyThread();
thread.start();
thread.interrupt();
System.out.println(“end of main thread”);
}
}
thread.interrupt();
|
Comment thread.interrupt();
|
i am lazy thread
|
i am lazy thread
|
i am lazy thread
|
i am lazy thread
|
.
|
.
|
.
|
.
|
i am lazy thread
|
i am lazy thread
|
i got interrupted
|
end of main thread
|
end of main thread
|
2. Why interrupt
- 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.
- A thread sends an interrupt by invoking interrupt() on the Thread object for the thread to be interrupted.
3. How many ways to interrupt a thread
- Once if we call interrupt() , than interrupt flag for that particular thread will be set.
- If a thread interrupt flag is true and it is in any one of the following methods , then thread will throw InterruptedException and clear the interrupt flag.
- wait(..)
- sleep(..)
- join(..)
- Interrupting a thread that is not alive need not have any effect.
- If the current thread cannot modify this thread then it will throw SecurityException (future classes detail explanation)
- If thread is blocked in I/O operations then the channel is closed , then thread interrupt flag will be set to true and throws ClosedByInterruptException
4. Can we check is interrupt flag for a particular thread is set or not.
- Yes, we can check by using isInterrupted()
public boolean isInterrupted()
5. Can we clear the interrupt flag.
- Yes, One more method is there to check interrupt flag is set or not , if set it will clear the flag automatically.
public static boolean interrupted()
- Above method clear the interrupt flag and also returns state of interrupt flag before modifying.
- This method will get and clear the currently executing thread's interrupted flag.(it is static)
6. Can we throw InterrupException without thread in any waiting or blocking states based on interrupt flag.
- Yes, We can check interrupt flag is set or not, once it is set clear the flag and throw InterruptException.
if (Thread.interrupted()) {
throw new InterruptedException(); } |
7. interrupt() vs isInterrupt() vs interrupted()
interrupt()
|
isInterrupt()
|
interrupted()
| |
Flag set
|
Sets the interrupt flag to true
|
It will not set
|
Set the interrupted flag to false
|
Syntax
|
Public void interrupt()
|
public boolean isInterrupted()
|
public static boolean interrupted()
|
Calling
|
We can call on any thread object
|
We can call on any thread object
|
We can call on currently executing thread.
|
returns
|
void
|
Returns interrupt flag
|
Returns interrupted flag
|
I have read your blog its very attractive and impressive,Thanks for sharing
ReplyDeleteJava Online Course