Reputation: 21450
will deadlock occur in these Java situations
1-
synchronized(obj) {
obj.syncMethod(); // the method signature: public synchronized void syncMethod() {...}
}
2-
synchronized(obj) {
if (condition)
throw new Exception(); // deadlock because obj lock is not released?
// do other stuff
}
Thank you.
Upvotes: 1
Views: 396
Reputation: 3417
Upvotes: 1
Reputation: 28049
If you don't catch the exception in your synchronized
block, then your lock will be released and no deadlock can occur. See here for details.
Upvotes: 0
Reputation: 6325
No deadlock shall occur. You're already holding the lock to obj.
If an exception is thrown the lock is released. See the question here on SO:
Side effects of throwing an exception inside a synchronized clause?
Upvotes: 2