Is there any way in which I could interrupt all the threads that are waiting for lock if the thread which already acquired lock has some exception?
Asked
Active
Viewed 295 times
0
-
The main reason I want to interrupt or even notify all the waiting threads is to make sure that the processing is consistent. My problem statement is I have a thread which calls a function and does some data processing and now within that function based on some condition am spawning another thread to call the same function and this could again spawn another thread if required. Now based on this if there is an exception in processing what I want is to notify/interrupt other threads who are waiting to execute the function that no need we have exception with the previous processing. – Akshay Srivastava Apr 20 '20 at 14:10
1 Answers
2
I don't think there is a good way.
If you created a subclass of ReentrantLock
, that class could call getQueuedThreads()
, iterate the resulting collection and call interrupt()
on each one. However, that is liable to be fraught with race conditions. For example, if another thread attempts to acquire the lock while you are doing this, there is no guarantee that you will see that thread.
The second problem is figuring where and by what the exception would be detected.

Stephen C
- 698,415
- 94
- 811
- 1,216
-
Thanks, so there isn't any clear way to do it. I will have to handle this condition on my own – Akshay Srivastava Apr 20 '20 at 14:06