This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amol,
I guess you can clear out your confusion by going through this tut-:
Thread Tut
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic