posted 22 years ago
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.
SCJP2. Please Indent your code using UBB Code