At the time of initializing a new Thread, after the method is executed run()
completely, how can I kill this thread?
public void iniciarHilo(){
new Thread(new Runnable() {
public void run() {
//Codigo
}
}).start();
//Una vez ejecutado el código del método run, necesito eliminar el hilo, o detenerlo
}
just like this
public void iniciarHilo(){
Thread hilo = new Thread(new Runnable() {
public void run() {
//Código
}
});
hilo.start();
//Eliminar este hilo
}
What happens is that every time a specific decision is fulfilled, I call a method that starts a new thread within it, to eliminate components of the frame with a small delay between each one and that it looks good, but the more I fall into that decision, the program gets stuck.
I think it's because the threads are running, because by removing the method thread and leaving only the process code, the program runs great.
You cannot command the execution of a thread to terminate. The only thing you can do is make sure that the execution of the method
run
(to be implementedRunnable
or extended fromThread
) has some way to finish. Upon completion of the execution of this method, the thread will terminate its execution. This is, instead of having something likeAt least have something like (this is actually too basic and not suitable for real-world applications, but it illustrates a way to stop and kill thread execution):
Since you mention the use of frames, I inform you that it is not recommended to create
Thread
s directly and modify the frames from there since Swing uses a different thread for the administration of the visual components. The best would be to useSwingWorker
, but for a better detail we would need to know more about your particular case.Forget the use of the
Thread#stop
and methodsThread#suspend
. The official documentation clearly explains that you should not use these methods (check the content of the links provided). There is even an article dedicated to this: Why areThread.stop
,Thread.suspend
andThread.resume
Deprecated? which translated means Why are theThread.stop
,Thread.suspend
and methodsThread.resume
deprecated? .stop()
This method causes the thread to stop immediately. This is often an abrupt way to stop a thread, especially if this method is executed on the current thread. In such a case, the line immediately after the stop() method call is never executed, because the thread context dies before stop() returns control. A more elegant way to stop a thread is to use some variable that causes the run() method to exit in an orderly manner. Actually, you should never resort to using this method.
suspend()
The suspend() method is different from stop(). suspend() takes the thread and causes it to stop executing without destroying the underlying system thread, nor the state of the previously running thread. If a thread's execution is suspended, resume() can be called on the same thread to get it to run again.
http://dhw.umh.es/alex-bia/teaching/PC/material/hilos_tutorial-java/cap10-2.htm
you can do something like this:
When you want to start or stop the thread, use the methods above, don't use thread.start(); or thread.stop();. You can also use the boolean to override the thread.isAlive();
Clarifications: you start the boolean as "volatile" so that while it is being read or written, its value cannot be modified. It is to avoid mistakes. And the methods to start and stop the thread, you start them as "synchronized" so that they cannot be executed at the same time, since it would cause an error.
I'll leave you the 3, I guess it's obvious what each one is for, although it's not a good practice to stop a thread and delete it, it should end naturally.
Cheers