Java interrupts or exits a thread
•
Java
I have a thread that performs several different tasks Each task depends on the success of the previous task
If this is the way I might write (long hand):
public boolean outerMethod() { boolean success= performTask(); if(success == false) { return false; } // more processing here if success == true }
And return the caller from outermethod without further processing
But
If I'm in the run () method of the thread, I'll do the following
How can I end the current thread there?
public void run() { boolean success = performTask(); if( success == false ) { /* here is where I want to exit this thread */ } // further processing if success == true }
Solution
You can simply call a return with no value to exit the void method ahead of time
public void run() { boolean success = performTask(); if( success == false ){ return; //ends the thread } // further processing if success == true }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码