Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unlocking lock owned by another thread java

Tags:

I have a LockManager that manages the locks of several threads. Sometimes the threads are bad boys, and I have to kill them and ask the LockManager to release all their locks. However, since I use ReentrantLock in java this is impossible, I can not unlock a lock owned by another thread.

I am forced to use Locks (cannot use semaphores, it is point of the homework). Is there any Java Lock implementation that allows me to unlock locks owned by other threads?

So far the options I considered are:

  • re-implementing ReentrantLock in a way that allows me to do this
  • Make some sort of mapping between Semaphores and ReentrantLocks

Extra Sources you may find useful:

  • Reentrant locks - Unlocking from another thread
  • Unlocking a lock from a thread which doesn't own it, or redesigning to avoid this?
like image 364
Flame_Phoenix Avatar asked May 10 '13 21:05

Flame_Phoenix


People also ask

How do you unlock a thread in Java?

unalock() with something like lockManager. release(lock) if you want the LockManager to call unlock, or you could make your LockManager provide your worker thread with some object that would be used to notify the thread has finished, and use it exactly as shown above (ie, try {...} finally { finishedFlag. set(); }.

How does ReentrantLock work in Java?

A ReentrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock.

What is tryLock in Java?

tryLock. boolean tryLock(long time, TimeUnit unit) throws InterruptedException. Acquires the lock if it is free within the given waiting time and the current thread has not been interrupted. If the lock is available this method returns immediately with the value true .

How many threads can possess a lock at the same time?

For a thread to work on an object, it must have control over the lock associated with it, it must “hold” the lock. Only one thread can hold a lock at a time. If a thread tries to take a lock that is already held by another thread, then it must wait until the lock is released.


1 Answers

You've discovered a major reason why common wisdom says: Don't kill threads!

Locks are only one of the potential resource leaks that can happen if you forcibly kill a thread. Consider open files and sockets, etc.

Also consider that if you did manage to unlock the lock, there was a reason the lock was locked in the first place. For example, the thread may have partially updated a data structure, and allowing access to that structure from another thread is likely to cause strange and wondrous program failures that are difficult if not impossible to debug.

The best way to handle this situation is to ask the thread to go away. Add a "stop()" method to the object associated with the thread (you do have an object for each thread, don't you?) that sets a flag, and have the thread check this flag regularly and exit if it is set.

If your threads are misbehaving in a way that prevents them from checking the stop flag, then the correct approach is to fix the code so that it does not misbehave.

like image 137
Dale Wilson Avatar answered Oct 01 '22 16:10

Dale Wilson