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
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.