0

I'm trying to create an Executor Service using a DelayQueue for the blocking queue with Java 8. I created a class that implements Runnable and Delayed, and implemented the required methods:

public class CurlEvent implements Runnable, Delayed {...} 

I constructed the queue, then the ExecutorService:

BlockingQueue<CurlEvent> queue = new DelayQueue<>(); ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 36, TimeUnit.HOURS, queue); 

The compiler flagged an error for the ThreadPoolExecutor constructor, even though CurlEvent implements Runnable:

java: incompatible types: java.util.concurrent.BlockingQueue<com.dtna.web.test.playback.CurlEvent> cannot be converted to java.util.concurrent.BlockingQueue<java.lang.Runnable> 

There must be some way around this. I can move to a later Java release if necessary

1 Answer 1

0

I found the answer here: How do you cast a List of supertypes to a List of subtypes?

In my case this worked out to:

 ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 36, TimeUnit.HOURS, (BlockingQueue<Runnable>)(BlockingQueue<?>) queue); 
Sign up to request clarification or add additional context in comments.

1 Comment

This is not a solution, this is a hack. The ThreadPoolExecutor will store implementations of Runnable into this queue which are not instances of CurlEvent. Therefore, the queue should not have the type BlockingQueue<CurlEvent> in the first place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.