I believe that the most difficult part is to identify the hanging thread. You do not provide information about this, but perhaps you can create some rules around the stream name or the current stack trace.
If you can identify a thread by its name, I would get all the threads in the virtual machine, getting my own thread group using Thread.currentThread().getThreadGroup() , then raise the thread group hierarchy by calling getParent() in the thread group, until it will not return null . You now have a top-level thread group. Now you can fill the pre-allocated array with all threads using the enumerate(Thread[] list) method in the top-level thread group.
If you need stack traces to identify a thread, you can also use the static utility method Map<Thread,StackTraceElement[]> Thread.getAllStackTraces() to get all the threads. Computing the stack trace, however, is quite expensive, so this might not be the best solution if you really don't need them.
After defining the stream, you must call the stop() method. Interrupting this will not help if the implementation of the executable code does not actually evaluate the thread interrupt flag and behaves as you expect. Not that the stop() method is deprecated and that using it can have a lot of funny side effects. You can find more information in the API documentation.
jarnbjo
source share