java - Pause ScheduledExecutorService -


i using scheduledexecutorservice execute task calls service @ fixed rate. service may return data task. task stores data in queue. other threads pick items queue

import java.util.concurrent.blockingqueue; import java.util.concurrent.linkedblockingqueue; import java.util.concurrent.scheduledexecutorservice; import java.util.concurrent.timeunit;  public class everlastingthread implements runnable {      private scheduledexecutorservice executorservice;     private int time;     private timeunit timeunit;     private blockingqueue<string> queue = new linkedblockingqueue<string>(500);      public everlastingthread(scheduledexecutorservice executorservice, int time, timeunit timeunit) {         this.executorservice = executorservice;         this.time = time;         this.timeunit = timeunit;     }      public void run() {          // call service. if service returns data put queue         queue.add("task");     }      public void callservice() throws exception {         // while queue has stuff dont exucute???????????          executorservice.scheduleatfixedrate(this, 0, time, timeunit);     } } 

how pause executorservice until queue populated task has been cleared.

when executor shudown, no longer accept new task , wait current ones terminate. don't want terminate executor, pause it.

so can do, in task deal empty queue. because task executed time time, cpu consumption near 0 when there no processing do. "if(!queue.isempty()) return;" peter lawrey response.

second, use blocking queue. mean if call method take() queued element while queue empty, executor thread wait until element added queue automatically.

so:

  • you can't pause executor complicate code.
  • a blocking queue design need: blocking task if queue empty.
  • if prefer can let periodic task run , check if queue isempty.
  • you should use 1 or other way in task anyway otherwise have nullpointerexception when queue empty.

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -