Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6314788

Tasks resubmitted to a ThreadPoolExecutor may fail to execute

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 6
    • core-libs

      Doug Lea writes:

      Re-submitting a task that is rejected but has also been
      previously successfully accepted may cause an instance of that task
      not to be run.

      Test program:


      import java.util.concurrent.*;
      import java.util.concurrent.atomic.*;

      public class RecycledTPETask {
           public static void main(String[] args) throws Exception {
               final int nTasks = 1000;
               final AtomicInteger nRun = new AtomicInteger(0);
               final Runnable recycledTask = new Runnable() {
                       public void run() {
                           nRun.getAndIncrement();
                       } };
               final ThreadPoolExecutor p =
                   new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
                                          new ArrayBlockingQueue(30));
               try {
                   for (int i = 0; i < nTasks; ++i) {
                       for (;;) {
                           try {
                               p.execute(recycledTask);
                               break;
                           }
                           catch (RejectedExecutionException ignore) {
                           }
                       }
                   }
                   Thread.sleep(5000); // enough time to run all tasks
                   if (nRun.get() < nTasks)
                       throw new Error("Started " + nTasks +
                                       " Ran " + nRun.get());
               } catch(Exception ex) {
                   ex.printStackTrace();
               } finally {
                   p.shutdown();
               }
           }
      }

            martin Martin Buchholz
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: