Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads " Watch "Threads " New topic
Author

Threads

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
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: 284
Amol,
I guess you can clear out your confusion by going through this tut-:
Thread Tut
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads
 
Similar Threads
Reg. Thread basics question
Synchronizing the Methods.
Thread example, dont know why is it a deadlock condition
Deadlocks in Threads?
tthread doubt