Interrupt
- Eclipse: Oxygen
- Java: 1.8
The basic concept of interrupt is that it stops the thread that is in an idle state by showing the interrupted exception. Therefore it can be given a chance to execute in case of a long wait or sleep functions.
Suppose there are two threads and If one of the threads is blocked in an invocation of the wait() method of this class, then its interrupt status will be cleared and it will receive an Interrupt Exception, which gives the chance to another thread to execute the corresponding run() method of another thread which results into high performance and reduces the waiting time of the threads.
In the following program, inside the withdraw method based on the condition we are simply calling wait() method without using any notify() or interrupt() method. Hence the program will not terminate by itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
public class App { static public int balance = 0; public void withdraw(int amount) { synchronized (this) { if (balance <= 0) { try { System.out.println("Waiting for balance updation"); wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } balance = balance - amount; System.out.println("Withdrawal successful and the current balance is: " + balance); } public void deposit(int amount) { System.out.println("We are depositing the amount"); balance = balance + amount; } public static void main(String[] args) { App app = new App(); Thread thread1 = new Thread(new Runnable() { @Override public void run() { app.withdraw(1000); } }); thread1.setName("Thread1"); thread1.start(); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } app.deposit(2000); } }); thread2.setName("Thread"); thread2.start(); } } |
Output
1 2 3 |
Waiting for balance updation We are depositing the amount |
If any thread which is in the waiting state can be interrupted with the help of interrupt() method.
Sometimes there can be a situation where the thread should not work continuously, but it can go in a wait state once a while, to give other threads a chance to do their work. But when a thread is waiting it can’t check actively whether it should terminate. Then we need to use interrupt() method. We can call the interrupt() method directly from the run() method.
Note: interrupt() method always derived by the reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
public class App { static public int balance = 5000; public void withdraw(int amount) { synchronized (this) { if (balance <= 0 || balance < amount) { try { System.out.println("Waiting for balance updation"); wait(); } catch (InterruptedException e) { System.out.println("original balance: "+balance); System.out.println("Withdrawal amount: "+amount); balance = balance - amount; System.out.println("Withdrawal successful and the current balance is: " + balance); } }else{ System.out.println("We are inside else"); } } } public void deposit(int amount) { System.out.println("We are depositing the amount "+amount); balance = balance + amount; } public static void main(String[] args) { App app = new App(); Thread thread1 = new Thread(new Runnable() { @Override public void run() { app.withdraw(1000); } }); thread1.start(); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } app.deposit(2000); thread1.interrupt(); } }); thread2.start(); } } |
Output
1 2 3 |
We are inside else We are depositing the amount 2000 |
Contributed by: Poonam Tomar