Actually the thread has a stop()
method, so you can view all the worker threads and call this method for each of them.
Question: "Where to get a list of workflows?" The best solution is to store this list somewhere at the application level, i.e. Each time you create a workflow, put it in a special list. This is the best solution, because you and only you know that the thread is a "work" thread.
But theoretically, you can even dynamically detect your application and get streams. There is a static method Thread.enumerate(Thread[] threads)
that populates the provided array. But how do you know how many threads are working now? Use Thread.activeCount()
.
Thread[] threads = new Thread[Thread.activeCount()]; Thread.enumerate(threads); for (Thread t : threads) { if (isWorkerThread(t)) { t.stop(); } }
You must define your workflows. For example, you can use a stream name or a thread stack trace for this.
BUT it is a crime to call the deprecated stop()
method. Please refer to the javadoc of this method for reasons.
The "right" method is to implement an elegant shutdown mechanism at the application level. Each thread should check some flag, which says whether the thread should turn off, and when the flag is true, just return from the run()
method. In this case, it is very simple to close worker threads: just set the value of this flag to true and the threads will stop. That's the right decision.
source share