How to stop threads created using an anonymous class?

In my program, I have many threads in working condition, running at the start of a method or by calling another method from run. What is the method of stopping these threads?

Streams start as:

Runnable r = new Runnable() { @Override public void run() { // do something... } }; new Thread(r,"Thread_1").start(); 

Another may be:

 Runnable r = new Runnable() { @Override public void run() { startThread(); // another method called } }; new Thread(r,"Thread_2").start(); 

What is the method of stopping thread_1 and thread_2 ?

UPDATE

What I want is when I click on the deactivate button, which has 123 threads working behind it, which must be deactivated so that the user can start these threads again by assigning the task again.

+4
source share
5 answers

Thread.stop() very dangerous, it is strongly recommended to avoid it for the reasons specified in javadoc.

Try passing the message to Runnable , perhaps using something like this:

 final AtomicBoolean terminate = new AtomicBoolean(false); Runnable r = new Runnable() { @Override public void run() { while (!terminate.get()) { // do something... } } }; new Thread(r,"Thread_1").start(); // some time later... terminate.set(true); 

You will need to figure out a way to cover the variables so that it can be compiled to fit your setup. Alternatively, extend Runnable your interface, which has a cancel() method (or something else) that your anonymous classes should implement, which looks like this with a loop check flag.

 interface CancellableRunnable extends Runnable { void cancel(); } 

 CancellableRunnable r = new CancellableRunnable() { private volatile terminate = false; @Override public void run() { while (!terminate) { // do something... } } @Override public void cancel() { terminate = true; } } new Thread(r,"Thread_1").start(); // some time later... r.cancel(); 
+5
source

The Thread class has a stop() method introduced in java 1.0 and deprecated in java 1.1. This method still works and kills the thread. This way you can stop all your threads if you want.

But this is highly discouraged. The reason is described in javadoc Thread.stop() . Thus, the best solution is to fix the implementation of your threads so that they can exit correctly. But if this is not possible (for example, if it is not your code), and you still need to use Thread.stop() very much.

+1
source

The problem with setting the flag and the thread periodically checking the flag to interrupt the thread is that if your thread calls a multi-year API method or a blocking method, such as the put method for a full BlockingQueue that will never be deleted, because the thread that should Was it deleted, closed or blocked by reading from a socket that will never be written to? In all such cases, none of these lengthy calls will ever stop checking your flag, and your application may hang.

The proper way to interrupt a thread is to execute the Thread.interrupt () method on the thread instance, and in the current thread you should periodically check Thread.currentThread (). isInterrupted () to determine if a thread should be interrupted and, of course, catch an InterruptedException and perform an ordered shutdown if it is thrown.

The advantage of this approach is that if your thread calls a long-term method, I hope this method will also check Thread.currentThread (). isInterrupted () will periodically and will respond to an interrupt request, performing an orderly shutdown, and then throwing an InterruptedException. For example, BlockingQueue.put will indeed detect an interrupt request issued by Thread.interrupt (). Sockets will not detect an interrupt request, but will respond to a socket closure, which can be done in the redefined thread () method.

Since there is no way to call the interrupt () method on an anonymous thread, you probably shouldn't use them for lengthy tasks.

I recommend reading "Java Concurrency in Practice." This is a great book, and Chapter 7, β€œOn and Off,” deals with this. That explains it a lot better than I can.

+1
source

If you want to force them to interrupt, you can call the stop() method (not recommended). Just make sure you don't open files, don't execute I / O sockets using locks, etc.

A better approach would be to have a while loop checking the flag inside the run:

 public void run() { while(!stop) { // work } } 

And set stop to true when you want to stop the stream. Remember to make it volatile .

0
source

As I understand it, you want to know how to detect a specific anonymous thread and stop it. You can try the getCurrentThread() method to detect the thread. Although stop() not safe to use, it is deprecated.

 Thread thread = Thread.getCurrentThread(); if(thread.getName() == "Thread_1" || thread.getName() == "Thread_2") { thread.stop(); } 
0
source

Source: https://habr.com/ru/post/1385745/


All Articles