我在其中包含了我main
方法爪哇 - 關閉線程與「迴歸」不工作
ExecutorService executor = Executors.newFixedThreadPool(1);
Runnable formatConcentration = new formatConcentration(87);
executor.execute(formatConcentration);
System.out.println("Called an instance of formatConcentration");
while (!executor.isTerminated())
{
//stay Alive
Thread.sleep(1000);
System.out.println("Still alive");
}
System.out.println("Program successfully finished");
return;
這將創建formatConcentration
類的實例類下面的代碼。其代碼如下(爲了示例,我已將所有功能都拿掉了)。
public class formatConcentration extends Thread{
private int numberOfNewRows;
formatConcentration(int rows)
{
this.numberOfNewRows = rows;
}
public void run() {
System.out.println("Running the Formatting Script");
final int numberOfNewRegistered = this.numberOfNewRows;
try
{
System.out.println("Finished formatting");
}//end of try
catch (Exception e1)
{
log.log(e1.toString());
log.closeLog() ;
System.exit(1) ;
}
System.out.println("Still Finished formatting");
return;
}
}
我的問題是一旦調用return
它不會終止線程。
我已經做了相當多的四處張望,據我可以告訴這應該沒問題,但我會想象,我忽略了一些小事,並會欣賞新問題的眼睛。
或者,如果有人對如何從run(){}
方法內殺死線程一個建議,我將不勝感激(最好不要通過設置,我會在主類檢查變量,但如果這就是如何我必須做的它是這樣),因爲我知道一旦它達到return語句就結束了,不再需要任何參考run()
中創建的變量。
生成到控制檯輸出如下:
Called an instance of formatConcentration
Running the Formatting Script
Finished formatting
Still Finished formatting
Still alive
Still alive
Still alive
Still alive
Still alive
etc.
executor.shutdown();在此之前 –