如何与Executor服务线程进行通信

时间:2017-09-13 19:00:16

标签: java multithreading executorservice

从控制器类中,我调用此Helper来启动进程并返回到已启动进程的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是一个巨大的列表,我将它分成批处理并进行处理。现在,如果我想停止这个过程,我该怎么做? 我认为在工作者类中应该有一个标志,如果我继续处理它,应该在每个分区运行后检查它。 但我不明白如何将这面旗帜传递给工人阶级。我创建了一个停止服务方法,我想创建一个volatile原子布尔标志并将其传递给startWork方法。但我想它只有在两者都是单例对象时才有效。由于singleton对象只有一个实例,我最终也可能会停止其他当前正在运行的服务。 (不确定,需要澄清)。

感谢。

1 个答案:

答案 0 :(得分:0)

在每个级别保留对ExecutorService的引用,以便可以调用shutdownNow()。例如:

    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表示Callable / Runnable必须表现良好,在中断时响应

例如:

    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();
        }
    }