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

Q1. What do you mean by VPN(virtual private network)? Q2. What do you mean by switching and what are it's type? @3. What do you mean by ATM switching?

3 Answers  


where is interrupt vector table is located??

1 Answers   Infosys,


char *a[2]; int const *p; int *const p; struct new { int a;int b; *var[5] (struct new)} Describe the statements in the above given construct ?

0 Answers   Geometric Software,


what are active and passive components

2 Answers  


how 2 write a program of this pattern usin for loop?? 1 2 3 4 5 6 7 8 9 10

0 Answers  






PLEASE SUGGEST THE BALLY PLACEMENT PAPER PATTERN ABOUT WRITTEN TEST,INTERVIEW,HR.ITS URGENT. AS BALLY IS GOING TO VISIT OUR CAMPUS.

0 Answers  


what is the differences between software engineering and other engineering disciplines.

0 Answers  


Discuss about WAP architecture in detail.

0 Answers  


2 ships are wardroom light house one side at 30 degree and other ship other side at 45 degree. If light house has 100 m, find distance of boat from light house. and also find distance of each boat from light house

2 Answers   3D PLM,


what is the difference between primary key and foreign key?

3 Answers   TCS,


Some question are asked from computers also. How we calculate sum in Excel? can we find cube root in Excel if yes , how? If i want to send excel sheet to 10 different person, how i can send it ( don`t tell me by mailing) condition given by interviewer.If A=.03rs(tsqr), value of r increase by 50%, s by 30% & t decrease by 20%. find value of A. If two coins having dia 25 & 16 they are rotate in their axis , Coin having dia 25 reach a point at x per sec, in how many min coin having dia 16 reach that place, Both are rotating in same direction.

0 Answers   NTPC,


Please derive the formula for the area of a circle.

0 Answers  


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)