I need to implement the following scenario
- ThreadN acquires a lock
- ThreadM tries to acquire the lock and wait
- ThreadX (monitor) sees that ThreadN is holding lock too long time and releases the lock
- ThreadM acquires the lock and continue
Fate of ThreadN does not matter.
Which classes (from java.util.concurrent ?) should I use ?
I think you can implement this based on
Semaphore, though you’ll have to implement the monitoring functionality yourself by setting a timestamp when a lock is acquired, checking it periodically and interrupting the thread holding the lock if it’s held too long.The
LockSupportclass also looks like it could help.