2015-05-15 24 views
0

我在其中包含了我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. 
+0

executor.shutdown();在此之前 –

回答

0
ExecutorService executor = Executors.newFixedThreadPool(1); 
Runnable formatConcentration = new formatConcentration(87); 
executor.execute(formatConcentration); 
System.out.println("Called an instance of formatConcentration"); 
executor.shutdown(); 
while (!executor.isTerminated()) 
{ 
    //stay Alive 
    Thread.sleep(1000); 
    System.out.println("Still alive"); 
} 
System.out.println("Program successfully finished"); 
return; 

另一種更簡單的方式來做到這將是:

ExecutorService executor = Executors.newFixedThreadPool(1); 
    Runnable formatConcentration = new formatConcentration(87); 
    executor.execute(formatConcentration); 
    executor.shutdown(); 
    try { 
     executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

執行人awaitTermination方法代替你的循環。

+0

我會用你的第二個解決方案。 出於好奇,因爲你稱之爲「executor.shutdown();」之前「while(!executor.isTerminated())」在它到達while循環之前不會總是關閉嗎?因此,因爲它不是一個守護進程線程,它將不會關閉,直到所有資源都交給回執使執行程序還活着的while循環無法訪問? –

+0

您可以調用shutdown方法,但該方法不會等待執行程序終止所有線程。關機調用後繼續執行。所以它會進入while循環,如果線程執行時間更長,它會執行幾個循環。 這就是爲什麼如果你想在關機後等待,你可以調用awaitTermination,它與循環基本相同,但在代碼中更清晰。 –

+0

最後一件事 - 使用大寫字母作爲班級的名字;) –

2

你永遠不關機executor。從Javadocs爲isTerminated

如果所有任務在關閉後完成,則返回true。請注意,除非shutdown或shutdownNow先被調用,否則isTerminated從來就不是真的。

只要您不關閉executor,您可以提交新的任務給它執行。 isTerminated確實不是檢查提交的任務的狀態,它檢查ExecutorService本身的狀態。

+0

擊敗我。 +1。 – Mena

0

ExecutorService主要用於將您的工作委託給separte工作人員。 使用這個只有大部分連接池庫已經創建。 因此,只要您的應用程序啓動並且將由您的應用程序的關閉方法關閉(手動),就會啓動此服務。

在你的程序,你應該寫代碼檢查執行服務的狀態,

是任何工人正在工作...... 如果沒有工人在忙碌模式則只是停止接受任何進一步的任務。

從javadocs中看到樣品關機代碼,

下面的方法關閉的ExecutorService的兩個階段,首先通過調用shutdown拒絕傳入任務,然後調用shutdownNow時,如果必要的話,取消任何揮之不去的任務:

void shutdownAndAwaitTermination(ExecutorService pool) { 
    pool.shutdown(); // Disable new tasks from being submitted 
    try { 
    // Wait a while for existing tasks to terminate 
    if (!pool.awaitTermination(60, TimeUnit.SECONDS)) { 
     pool.shutdownNow(); // Cancel currently executing tasks 
     // Wait a while for tasks to respond to being cancelled 
     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) 
      System.err.println("Pool did not terminate"); 
    } 
    } catch (InterruptedException ie) { 
    // (Re-)Cancel if current thread also interrupted 
    pool.shutdownNow(); 
    // Preserve interrupt status 
    Thread.currentThread().interrupt(); 
    } 
} 

希望它會給你一些信息。