Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Thread Synchronization

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me to explain below code. As per my understanding this should be a deadlock condition.

Source of this example(U-certify question Bank)

public class CheckNeavigableCode {
public static void main(String ar[]) throws InterruptedException {
String str1 = new String("R1");
String str2 = new String("R2");
MyThread thrd1 = new MyThread("Firsr", str1, str2);
MyThread thrd2 = new MyThread("Second", str1, str2);
thrd1.start();
thrd1.join();
thrd2.start();
}
}

class MyThread extends Thread {
private String First;
private String Second;

public MyThread(String thrdname, String one, String two) {
super(thrdname);
First = one;
Second = two;
}

public void run() {
if (getName().equals("First")) {
synchronized (First) {
try {
Thread.sleep(4000);
} catch (InterruptedException E) {
E.printStackTrace();
}
synchronized (Second) {
}
}
} else {
synchronized (Second) {
try {
Thread.sleep(4000);
} catch (InterruptedException E) {
E.printStackTrace();
}
synchronized (First) {
}
}
}
}
}
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The main thread starts thread 1, then it waits for thread 1 to finish, then it starts thread 2. The two threads are never running concurrently.

Henry
 
Garg Amit
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply.

Without thrd1.join(); would it be a deadlock?
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep.
 
Sanju Thomas
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you change the line given below

MyThread thrd1 = new MyThread("Firsr", str1, str2);

to

MyThread thrd1 = new MyThread("First", str1, str2);

then it should create a dead lock.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic