为什么不能构造一个由DelayQueue支持的ThreadPoolExecutor? [英] Why can't I construct a ThreadPoolExecutor backed by a DelayQueue?

查看:180
本文介绍了为什么不能构造一个由DelayQueue支持的ThreadPoolExecutor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个ThreadPoolExecutor:

I'm trying to create a ThreadPoolExecutor:

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

编译器说找不到符号:

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

但我不明白- DelayQueue 实现了 BlockingQueue ,所以我不应该使用此构造函数

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

推荐答案

这是一个泛型问题。您不能使用 DelayQueue< Thingy> ,它必须是 DelayQueue< Runnable> 作为 ThreadPoolExecutor 构造函数未声明为接受 Runnable 子类型的队列。

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.

这篇关于为什么不能构造一个由DelayQueue支持的ThreadPoolExecutor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆