Can any1 say how this prog logic works... i am weak in thread
concept...pls explain step by step



Can any1 say how this prog logic works... i am weak in thread concept...pls explain step by step..

Answer / nachiyappan

class A {

synchronized void foo(B b) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered A.foo");
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("A Interrupted");
}
System.out.println(name + " trying to call
B.last()");
b.last();
}

synchronized void last() {
System.out.println("Inside A.last");
}
}

class B {

synchronized void bar(A a) {
String name = Thread.currentThread().getName();
System.out.println(name + " entered B.bar");
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println("B Interrupted");
}
System.out.println(name + " trying to call
A.last()");
a.last();

}
synchronized void last() {
System.out.println("Inside A.last");
}
}

class Deadlock implements Runnable {

A a = new A();
B b = new B();

Deadlock() {
Thread.currentThread().setName("MainThread");
Thread t = new Thread(this, "RacingThread");
t.start();
a.foo(b); // get lock on a in this thread.
System.out.println("Back in main thread");
}

public void run() {
b.bar(a); // get lock on b in other thread.
System.out.println("Back in other thread");
}

public static void main(String args[]) {
new Deadlock();
}
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Engineering AllOther Interview Questions

tcs aptitude questions

1 Answers   TCS,


what are nanoemulsions and how are they prepared? i want a brief outline about nanoemulsions.. is anyone of you is having any literature, can you share that? also any an idea where i can get the information?

0 Answers  


Dear sir 1: i know about IIT & AIEEE prepration which city is best and top 1 position ? 2: Top 10 institute in INDIA for prepration to IIT & AIEEE plz get me detail knowldge? 3: where is better invoirnment availble for IIT & AIEEE entrance exam? 4: What diffrent between KOTA & DELHI institute and enviornment

1 Answers  


Write steps of retrieving data using ado.net?

0 Answers  


how much the percentile as a good percentile in elitmus.

0 Answers  






what is advantages and disadvantages of vlan?

0 Answers  


Why Mother Board is called Mother Board & not Father Board?

12 Answers   Microsoft, Precision Infomatic, SoftStream,


i want to section engineer(signal) question paper......plz tell me how i get

0 Answers  


why every computer should have a boot strap routine?

0 Answers  


give me a example of an application of Low severity & high priority.

0 Answers   DataCore, DCPL,


can anyone help me with an article or a few pointers on what are the industry expectations from Fresh engineers in today's market scenario

0 Answers  


Always declare destructors to be virtual"?????? why this is so needed?????

0 Answers   TCL,


Categories
  • Civil Engineering Interview Questions Civil Engineering (5085)
  • Mechanical Engineering Interview Questions Mechanical Engineering (4451)
  • Electrical Engineering Interview Questions Electrical Engineering (16632)
  • Electronics Communications Interview Questions Electronics Communications (3918)
  • Chemical Engineering Interview Questions Chemical Engineering (1095)
  • Aeronautical Engineering Interview Questions Aeronautical Engineering (239)
  • Bio Engineering Interview Questions Bio Engineering (96)
  • Metallurgy Interview Questions Metallurgy (361)
  • Industrial Engineering Interview Questions Industrial Engineering (259)
  • Instrumentation Interview Questions Instrumentation (3014)
  • Automobile Engineering Interview Questions Automobile Engineering (332)
  • Mechatronics Engineering Interview Questions Mechatronics Engineering (97)
  • Marine Engineering Interview Questions Marine Engineering (124)
  • Power Plant Engineering Interview Questions Power Plant Engineering (172)
  • Textile Engineering Interview Questions Textile Engineering (575)
  • Production Engineering Interview Questions Production Engineering (25)
  • Satellite Systems Engineering Interview Questions Satellite Systems Engineering (106)
  • Engineering AllOther Interview Questions Engineering AllOther (1379)