Interrupting a
Thread:
The 3 methods provided by the Thread class for
interrupting a thread
Example of interrupting a thread that stops working
1. class TestInterruptingThread1 extends Thread{
2. public void run(){
3. try{
4. Thread.sleep(1000);
5. System.out.println("task");
6. }catch(InterruptedException e){
7. throw new RuntimeException("Thread interrupted..."+e);
8. }
9.
10. }
11.
12. public static void main(String args[]){
13. TestInterruptingThread1 t1=new TestInterruptingThread1();
14. t1.start();
15. try{
16. t1.interrupt();
17. }catch(Exception e){System.out.println("Exception handled "+e);}
18.
19. }
20. }
Output:Exception in thread-0 java.lang.RuntimeException: Thread interrupted... java.lang.InterruptedException: sleep interrupted at A.run(A.java:7)
Example of interrupting a thread that doesn't stop
working
1. class TestInterruptingThread2 extends Thread{
2. public void run(){
3. try{
4. Thread.sleep(1000);
5. System.out.println("task");
6. }catch(InterruptedException e){
7. System.out.println("Exception handled "+e);
8. }
9. System.out.println("thread is running...");
10. }
11.
12. public static void main(String args[]){
13. TestInterruptingThread2 t1=new TestInterruptingThread2();
14. t1.start();
15.
16. t1.interrupt();
17.
18. }
19. }
Output:Exception handled java.lang.InterruptedException: sleep interrupted thread is running...
Example of interrupting thread that behaves normally
1. class TestInterruptingThread3 extends Thread{
2.
3. public void run(){
4. for(int i=1;i<=5;i++)
5. System.out.println(i);
6. }
7.
8. public static void main(String args[]){
9. TestInterruptingThread3 t1=new TestInterruptingThread3();
10. t1.start();
11.
12. t1.interrupt();
13.
14. }
15. }
Output:1 2 3 4 5
What about isInterrupted and interrupted method?
1. public class TestInterruptingThread4 extends Thread{
2.
3. public void run(){
4. for(int i=1;i<=2;i++){
5. if(Thread.interrupted()){
6. System.out.println("code for interrupted thread");
7. }
8. else{
9. System.out.println("code for normal thread");
10. }
11.
12. }//end of for loop
13. }
14.
15. public static void main(String args[]){
16.
17. TestInterruptingThread4 t1=new TestInterruptingThread4();
18. TestInterruptingThread4 t2=new TestInterruptingThread4();
19.
20. t1.start();
21. t1.interrupt();
22.
23. t2.start();
24.
25. }
26. }
Output:Code for interrupted thread code for normal thread code for normal thread code for normal thread |
Reentrant Monitor in Java
According to Sun Microsystems, Java monitors are reentrant means java thread can reuse the same
monitor for different synchronized methods if method is called from the method.
Advantage of Reentrant
Monitor
It eliminates the possibility
of single thread deadlocking
Let's understand the java
reentrant monitor by the example given below:
1. class Reentrant {
2. public synchronized void m() {
3. n();
4. System.out.println("this is m() method");
5. }
6. public synchronized void n() {
7. System.out.println("this is n() method");
8. }
9. }
In this class, m and n are the
synchronized methods. The m() method internally calls the n() method.
Now let's call the m() method
on a thread. In the class given below, we are creating thread using annonymous
class.
1. public class ReentrantExample{
2. public static void main(String args[]){
3. final Reentrant re=new Reentrant();
4.
5. Thread t1=new Thread(){
6. public void run(){
7. re.m();//calling method of Reentrant class
8. }
9. };
10. t1.start();
11. }}
Output: this is n() method
this is m() method
No comments:
Post a Comment