代码之家  ›  专栏  ›  技术社区  ›  Kid101

如何与Executor服务线程通信

  •  0
  • Kid101  · 技术社区  · 8 年前

    在控制器类中,我调用这个助手来启动一个进程,并返回到该进程启动的UI

    public class Helper {
    
    public String startService() { //Before starting the service I save the status of the service as Started in the DB
    
        ExecutorService service = Executors.newSingleThreadExecutor();
        service.submit(new  Runnable() {
            public void run() {
            new Worker().startWork(callableTaskList);   
                }
            });
    return "started"
        }
    public void stopService() { 
    // I Saved the status in DB as Stopping (Just in case). but now how to pass flag an to pass to startWorkMethod to stop if some flag in false and stop processing.
    }
    

    工人阶级

      public class Worker {
    
        public void startWork(List<CallableTask> callableTaskList) throws Exception {
            ExecutorService service=Executors.newFixedThreadPool(50);
            ExecutorService anotherService=Executors.newFixedThreadPool(50);
    for (List<CallableTask> partition : Iterables.partition(callableTaskList, 500)){
              // do some work here and then return
                List<Future<String>> futures=service.invokeAll(partition );
                for(Future<String> future: futures){
                    anotherService.submit(new Task(future.get()));
                }
            }
    

    现在我的问题是如何停止已经启动的服务?由于callableTaskList是一个巨大的列表,我将其分为多个批次并进行处理。现在,如果我想停止这个过程,我该怎么做? 我认为在worker类中应该有一个标志,如果我应该继续处理这个问题,那么在每次运行分区后都应该检查它。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   Andrew S    8 年前

        public class Helper {
            private ExecutorService service;
    
            public String startService() {
               // ExecutorService service = Executors.newSingleThreadExecutor();
                service = Executors.newSingleThreadExecutor();
                service.submit(new  Runnable() {
                    public void run() {
                        new Worker().startWork(callableTaskList);   
                    }
                });
                return "started"
            }
    
        public void stopService() { 
            service.shutdownNow();
        }
    }
    

    然而,为了使其发挥作用 API 指示可调用/可运行必须行为良好,并且 中断时响应

        public class Worker {
            private ExecutorService service;
            private ExecutorService anotherService;
    
            public void startWork(List<CallableTask> callableTaskList) throws Exception {
                service=Executors.newFixedThreadPool(50);
                anotherService=Executors.newFixedThreadPool(50);
    
                for (List<CallableTask> partition : Iterables.partition(callableTaskList, 500)){
                    checkInterruptStatus(); 
    
    
                    // do some work here and then return
                    List<Future<String>> futures=service.invokeAll(partition );
                    for(Future<String> future: futures){
                        checkInterruptStatus();
    
                        anotherService.submit(new Task(future.get()));
                    }
                }
            }
    
            private void checkInterruptStatus() throws InterruptedException {
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException();
                } 
            }
    
            public void stopService() {
                service.shutdownNow();
                anotherService.shutdownNow();
            }
        }