【问题标题】:Java Multithread. How to halt the threads and to free the launcher?Java 多线程。如何停止线程并释放启动器?
【发布时间】:2012-11-28 22:05:44
【问题描述】:

我有这个使用:Spring MVC (Controller + Service + ThreadPoolTask​​Executor)、Callable 和 Future。

@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. Controller: stop-method 调用 Service: stop-method
  3. ¿ Service: stop-method 关闭并通知 Controller: launch-method

我需要编写步骤 3 的代码,以通知 Controller:launch-method 该过程已完成。

我不知道为什么 threadPoolTask​​Executor.shutdown() 没有释放 Controller:launch-method,它正在等待:result = future.get(); em> 并且没有引发异常。

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

有什么建议吗?

【问题讨论】:

    标签: java multithreading spring-mvc future callable


    【解决方案1】:

    您可以使用observer-observable 模式进行设计。其中控制器将是Observer,服务将是Observable。一旦服务停止,它将触发setChanged(); and notifyObservers(); 通知控制器它已完成。

    【讨论】:

    • 谢谢,但是Controller和Service之间的通信是通过正在等待Service方法结果的Future对象建立的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多