java - Single threading a task without queuing further requests -


i have requirement task executed asynchronously while discarding further requests until task finished.

synchronizing method queues tasks , doesn't skip. thought use singlethreadexecutor queues tasks well. looked @ threadpoolexecutor reads queue task executed , therefore have 1 task executing , minimum of 1 task queued (the others can discarded using threadpoolexecutor.discardpolicy).

the thing can think off use semaphore block queue. i've come following example show i'm trying achieve. there simpler way? have missed obvious?

import java.util.concurrent.*;  public class threadpooltester {     private static executorservice executor = executors.newsinglethreadexecutor();     private static semaphore processentry = new semaphore(1);      public static void main(string[] args) throws interruptedexception {         (int = 0; < 20; i++) {             kickoffentry(i);              thread.sleep(200);         }          executor.shutdown();     }      private static void kickoffentry(final int index) {         if (!processentry.tryacquire()) return;         executor.             submit(                 new callable<void>() {                     public void call() throws interruptedexception {                         try {                             system.out.println("start " + index);                             thread.sleep(1000); // pretend work                             system.out.println("stop " + index);                             return null;                          } {                             processentry.release();                         }                     }                 }             );     } } 

sample output

start 0 stop 0 start 5 stop 5 start 10 stop 10 start 15 stop 15 

taking axtavt's answer , transforming above example gives following simpler solution.

import java.util.concurrent.*;  public class syncqueuetester {     private static executorservice executor = new threadpoolexecutor(1, 1,              1000, timeunit.seconds,              new synchronousqueue<runnable>(),             new threadpoolexecutor.discardpolicy());      public static void main(string[] args) throws interruptedexception {         (int = 0; < 20; i++) {             kickoffentry(i);              thread.sleep(200);         }          executor.shutdown();     }      private static void kickoffentry(final int index) {         executor.             submit(                 new callable<void>() {                     public void call() throws interruptedexception {                         system.out.println("start " + index);                         thread.sleep(1000); // pretend work                         system.out.println("stop " + index);                         return null;                     }                 }             );     } } 

it looks executor backed synchronousqueue desired policy want:

executor = new threadpoolexecutor(     1, 1,      1000, timeunit.seconds,      new synchronousqueue<runnable>(),     new threadpoolexecutor.discardpolicy()); 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -