javacompiler-errorsexecutors

Why can't I construct a ThreadPoolExecutor backed by a DelayQueue?


I'm trying to create a ThreadPoolExecutor:

// Thingy implements Delayed and Runnable
ExecutorService executor = new ThreadPoolExecutor(1, 1, 0l, TimeUnit.SECONDS, new DelayQueue<Thingy>());

The compiler is saying "cannot find symbol":

symbol  : constructor ThreadPoolExecutor(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.DelayQueue<Thingy>)

but I don't understand — DelayQueue implements BlockingQueue, so shouldn't I be able to use this constructor?


Solution

  • This is a generics problem. You can't use DelayQueue<Thingy>, it has to be DelayQueue<Runnable> as the ThreadPoolExecutor constructor is not declared to accept queues of sub-types of Runnable.