• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

A deadlock thread question.

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A. The program prints �ABBCAD�
B. The program prints �CDDACB�
C. The program prints �ADCBADBC�
D. The output is a non-deterministic point because of a possible deadlock condition.
E. The output is dependent on the threading model of the system the program is running on.
The answer is B,D. But I don't know why the B is correct when D is correct. In D, it said it is non-deterministic the print out sequence.
Anyone would explain it? thx.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This situation should be recognized visually as a possible deadlock:
aThread grabs lock1
aThread grabs lock2
anotherThread grabs lock2
anotherThread grabs lock1
That is, several threads are locking the same locks in different order.
The deadlock could occur if:
aThread grabs lock1 and anotherThread is scheduled before it can grab lock2. anotherThread grabs lock2 but it cannot continue because lock1 is owned by aThread, thus it blocks waiting for lock1 tob released. Now aThread is scheduled again and tries to get lock2, but it blocks because it is already hold by anotherThread. Both threads are blocked waiting for each other to release the locks. Both threads are blocked without hope to progress.
 
mooooooo ..... tiny ad ....
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!