java - how to communicate with Executor Service Threads -


from controller class, i'm calling helper start process , returning ui process started

helper class:

public class helper {  public string startservice() { //before starting service save status of service started in db      executorservice service = executors.newsinglethreadexecutor();     service.submit(new  runnable() {         public void run() {         new worker().startwork(callabletasklist);                }         }); return "started"     } public void stopservice() {  // saved status in db stopping (just in case). how pass flag pass startworkmethod stop if flag in false , 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); (list<callabletask> partition : iterables.partition(callabletasklist, 500)){           // work here , return             list<future<string>> futures=service.invokeall(partition );             for(future<string> future: futures){                 anotherservice.submit(new task(future.get()));             }         } 

now question how can stop service has been started? since callabletasklist huge list i've divided batches , processing it. if want stop process how can that? think there should flag in worker class should checked after every partition run if should continue working on this. don't understand how pass flag worker class. created stop service method think of creating volatile atomic boolean flag , pass startwork method. guess work if both of them singleton objects. , since singleton object have 1 instance may end stopping other running services well. (not sure, need clarification).

thanks.

at each level keep reference executorservice shutdownnow() can invoked. 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, work api indicates callable/runnable must behaved , respond when 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);              (list<callabletask> partition : iterables.partition(callabletasklist, 500)){                 checkinterruptstatus();                    // work here , 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();         }     } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -