• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 23951
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.
reply
    Bookmark Topic Watch Topic
  • New Topic