aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Threads " Watch "Threads " New topic
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Threads
 
Similar Threads
tthread doubt
Reg. Thread basics question
Deadlocks in Threads?
Synchronizing the Methods.
Thread example, dont know why is it a deadlock condition