简体   繁体   中英

how to communicate with Executor Service Threads

From the controller Class, I'm calling this Helper to start a process and returning to the UI that process started

Helper Class:

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.
}

Worker Class

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

Now my question is how can I stop the service that has been started? Since callableTaskList is a huge list I've divided it into batches and processing it. Now if I want to stop the process how can I do that? I think there should be a flag in the worker class that it should be checked after every partition run if I should continue working on this. But I don't understand how to pass this flag to the worker class. I created a stop service method I think of creating a volatile atomic boolean flag and pass it to the startWork Method. but I guess it will only work if both of them are singleton objects. and since singleton object have only one instance I may end up stopping other currently running services as well. (Not Sure, Need clarification).

Thanks.

At each level keep a reference to the ExecutorService so shutdownNow() can be invoked. For example:

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

However, for this to work the API indicates the Callable/Runnable must be well behaved and respond when it is interrupted .

For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM