Why deadlock is not prevented upon detection with a runtime exception by the JVM?

Viewed 130

Java can detect a deadlock that is about to happen (or at least in some cases)

For example, consider the following:

Thread A acquires Lock A
Thread B acquires Lock B
Thread A tries to acquire Lock B - and blocks
Thread B tries to acquire Lock A - and blocks indefinitely

In the last statement, the JVM knows that it will end up in a deadlock (because it knows which thread holds which lock).

So my question is, why the JVM doesn't throw a runtime error upon the lock acquisition that will cause a deadlock, resulting in termination of the thread (like any other unhandled exception), and releasing its locks, again, like normal.

For example, in a database, if a transaction tries to lock some record and the DB figures out it will be deadlocked, that transaction is aborted. The same can be applied to threads, so why not?

0 Answers
Related