• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt on deadlock situation?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
The below code leads deadlock situation. I didn't understood why it goes to deadlock situation. Can any one please explain clearly.



package threads;

public class DeadLockDemo1 {
String o1 = "First";
String o2 = "Second";
static DeadLockDemo1 dlDemo = null;
Thread t1 = (new Thread(){
public void run(){
while(true){
synchronized(dlDemo){
System.out.println("dl1");
}
}
}
});
Thread t2 = (new Thread(){
public void run(){
while(true){
synchronized(dlDemo){
System.out.println("dl2");
}
}
}
});
public static void main(String[] args) {
dlDemo = new DeadLockDemo1();
dlDemo.t1.run();
dlDemo.t2.run();
}
}

Thanks,
Ravindranath.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As it is, the code can't deadlock, because it doesn't create any threads. That's because it calls the run methods, which by themselves do not execute within a new thread. To actually start new threads, you need to call the start methods.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's move this back to the SCJP forum...
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

and even if the threads are started, in this program they will never deadlock.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic