• 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

Question about thread

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,I came cross a difficult problem I thought.
and I ran it on JB and worked out "ABABCD".But I don't know Why?
Who can explian it in detail?
Thank u in advance


Which two statements are true? (Choose Two)
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 rigth answers are D and E.
PLS tell me why ?
[ February 16, 2003: Message edited by: frank yang ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank,
First of all - guys, please do indent your code. It will make understanding your questions much simpler.
As for the questions. Yes, the code presented is a classical example of the code which may cause deadlocks. Let's call threads thr1 and thr2. Imagine the following scenario. Thr1 starts and gets the exclusive lock on s1 (this is what synchronized(s1) means). It is just about executing the line "synchronized(s2)" but then JVM decides that the next thread (thr2) should have a chance to run. So it takes cpu away from thr1 and starts thr2. Thr2 gets exclusive lock on s2 and then tries to get an exclusive lock on s1. But s1 is already owned by thr1. So thr2 waits on attempt of getting the lock on s1. When CPU is given to thr1 again it tries to get a lock on s2. But it is locked by thr2. So thr1 waits. And this is the deadlock. Thr1 is locking s1 and is waiting for thr2 to release the lock on s2 and thr2 is locking s2 and is waiting for thr1 to release the lock on s1. Both threads are blocked so none of them can actually release the locked resource.
The behaviour depends on implementation of threads on a particular platform. There are 2 ways of scheduling threads (deciding which thread to run) - time sliced and preemptive. Preemptive means each thread, while given CPU, runs to its end. Other threads have to white for this thread to finish. Time sliced means each thread will get CPU for a certain amount of time. When the time is over, CPU is given to another thread. As you see, the deadlock is more likely to happen in the time sliced case. For preemptive scheduling, each thread will do its stuff to the end and they will not interfere with each other.
Hope that helps.
 
frank yang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u
Best regards
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic