my dog learned polymorphism
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Locking mechanism. What if a Thread dies? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Locking mechanism. What if a Thread dies?" Watch "Locking mechanism. What if a Thread dies?" New topic

Locking mechanism. What if a Thread dies?

Peter P. Fischer

Joined: Apr 21, 2006
Posts: 1
Hello all,

I've been working for a while in the locking mechanism on the B&S assignment. It's been interesting to read some others points of view regarding the possible solutions. However I have a question about a "dead Thread" (perhaps I haven't look at the forums deeply).

Suppose a simple example where two threads (T1 and T2) are trying to access a lock for writing. Let's say that T1 gets the lock, thus T2 must wait until T1 releases that lock. However T1 dies (what I mean with "dies" is that T1 does not finalize correctly). In this case T2 won't ever get the lock. So which are the possible solutions?

I've been thinking on three possible solutions:
(A) - The use of a WeakReference (as I've read in some books), thus letting the garbage collector to cope with dead threads. Although I think this is the most elegant solution, unfortunately I haven't found a simple solution in my particular case.
(B) - Use some kind of TIME_STAMP to check if the time since the lock has been taken falls within this interval, otherwise assume that the thread holding the lock has died.
(C) - State and discuss in the documentation that although you can have the problem discussed above, the solution is out of the scope of the assignment.

Any comments and solutions will be very welcome.
sumuhan umamaheswarampillai

Joined: Jun 19, 2002
Posts: 12
I think choice C is fine. That's what I did

I agree. Here's the link:
subject: Locking mechanism. What if a Thread dies?
jQuery in Action, 3rd edition