Reputation: 4645
How to hold multiple locks with wait/notify?
Imagine a situation with two lockable resources: printer and scanner. I ran into deadlock so, I decided to acquire the scanner lock before the printer lock. Now if I want the printer only, I still need to lock the scanner. Here the situation is that one (printing) thread gets to the printer, it notices that another the another (feeding) thread needs to feed paper in. My design calls for the printing thread to wait on the feeding thread to feed paper in.
If I wait on printer. I think I"m still holding the lock on scanner. How could Thread 2 enter the notification code?
What is the commonly used design that would work in this sort of a situation? Say I need to hold 2 locks (to avoid deadlock). How do I do wait/notify with both locks held? Sample code is below.
One obvious method is to invert the lock acquisition order throughout the code and hope that I don't ever need to wait on the scanner.
Is there another way out?
Sample code: Printing Thread:
synchronized (scanner) {
synchronized (printer) {
// action
while (trayEmpty) {
printer.wait();
}
}
}
Sample Code for Feeding Thread:
synchronized (scanner) {
synchronized (printer) {
// action
trayEmpty=false;
printer.notify();
}
}
Upvotes: 3
Views: 1059
Reputation: 7899
Is there another way out?
Rather than using wait/notify i would suggest you to use Java Concurrent Api explicit Locks there you can lock
and unlock
in sequence manner
Upvotes: 1
Reputation: 23552
One idea for this specific case would be to use a single lock. For example, create a lock object on the object that is at the intersection of row/col (e.g., create a matrix of lock objects).
Upvotes: 0