2012-12-11 135 views
1

我有這個使用:Spring MVC(Controller + Service + ThreadPoolTask​​Executor),Callable和Future。Java多線程。如何停止線程並釋放啓動器?

我有
@Controller 
    launch-method [invokes Service:launch-method and get result with Future] 
    stop-method  [invokes Service:stop-method] 

@Service [to launch async Tasks] 
    launch-method [Loop with threadPoolTaskExecutor.submit(callable)] 
    stop-method  [threadPoolTaskExecutor.shutdown()] 

停止事件流是:

  1. 用戶點擊停止按鈕,並調用一個控制器:停止-方法
  2. 控制器:停止-方法調用服務:停止方法
  3. ?該服務:停止法關機並通知控制器:發射法

我需要編寫第3步,通知到控制器:發射法,這一進程已經完成。

我不知道爲什麼threadPoolTask​​Executor.shutdown()不會釋放控制器:發射法,這是在等待:結果=的Future.get();並沒有引發異常。

try { 
    for (Future<String> future : sentResult) { 
     result = future.get(); 
     ... 
    } 
} catch (ExecutionException e) {...} 
    catch (InterruptedException e) {...} 
    catch (CancellationException e) {...} 
    catch (Exception e) {...} 

有什麼建議嗎?

回答

0

您可以設計observer-observable模式。其中控制器將爲Observer,服務將爲Observable。一旦服務停止,它將觸發setChanged(); and notifyObservers();通知控制器它已完成。

+0

謝謝,但Controller和Service之間的通信是使用正在等待Service方法結果的Future對象來建立的 –