| Author |
Threads
|
Satyajeet Kadam
Ranch Hand
Joined: Oct 19, 2006
Posts: 202
|
|
http://www.roseindia.net/java/thread/deadlocks.shtml Q1 Please explain this code? Q2)why this example casuses deadlock? Both threads m,m1 are having their own run method? q3)what is use of writing synchronized wtihin synchronized in run method? As per i think writing "synchronized" will allow only one thread to enter into critical region. What is use secound sysnchronized word in run ()? <code> class DeadDemo{ public static void main(String args[]){ String s1="Dead"; String s2="Lock"; MyThread1 m=new MyThread1(s1,s2); MyThread2 m1=new MyThread2(s1,s2); } } class MyThread1 extends Thread{ String s1; String s2; MyThread1(String s1, String s2){ this.s1=s1; this.s2=s2; start(); } public void run(){ while(true){ synchronized(s1){ synchronized(s2){ System.out.println(s1+s2); } } } } } class MyThread2 extends Thread{ String s1; String s2; MyThread2(String s1,String s2){ this.s1=s1; this.s2=s2; start(); } public void run(){ while(true){ synchronized(s2){ synchronized(s1){ System.out.println(s2+s1); } } } } } </code>
|
 |
Pranav Bhatt
Ranch Hand
Joined: Mar 20, 2006
Posts: 283
|
|
Amol, I guess you can clear out your confusion by going through this tut-: Thread Tut
|
 |
 |
|
|
subject: Threads
|
|
|