我有一个执行几个不同任务的线程。每个任务都取决于上一个任务是否成功。
如果这是我可以写的方法(长手):
public boolean outerMethod()
{
boolean success= performTask();
if(success == false)
{
return false;
}
// more processing here if success == true
}
并从outerMethod
后面传回给呼叫者,并且不会进行进一步的处理
但...
如果我处于run()
线程方法中,并且执行如下所示的操作...
我如何才能在那里结束当前线程?
public void run()
{
boolean success = performTask();
if( success == false )
{
/* here is where I want to exit this thread */
}
// further processing if success == true
}
您可以简单地调用return
没有值的void
方法来更早退出方法。
public void run() {
boolean success = performTask();
if( success == false ){
return; //ends the thread
}
// further processing if success == true
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句