使用执行程序创建ScheduledThreadPoolExecutor

编程入门 行业动态 更新时间:2024-10-26 15:21:58
本文介绍了使用执行程序创建ScheduledThreadPoolExecutor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对于为什么以下强制转换不起作用感到非常困惑:

I am extremely confused as to why the following cast does not work:

ScheduledThreadPoolExecutor timeoutControl = (ScheduledThreadPoolExecutor) Executors.newSingleThreadScheduledExecutor();

ScheduledThreadPoolExecutor实现ScheduledExecutorService.如果我不能在实际的课程中使用此执行器,那有什么意义呢?

ScheduledThreadPoolExecutor implements the ScheduledExecutorService. What's the point of this Executors call if I can't use it with an actual class.

我使用错了吗(可能是),有人可以提供一些指导吗?

Am I using it wrong (probably), could someone offer some guidance please?

推荐答案

问题是Executors.newSingleThreadScheduledExecutor();实际上不返回ScheduledThreadPoolExecutor.

Executors类中的28%29"rel =" nofollow>源代码:

Source code in the Executors class:

public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1)); }

Delegated...类(也有一个DelegatedExecutorService)只是将所有调用传递给基础执行程序,在本例中为ScheduledThreadPoolExecutor.代码中的注释表明,这些类的全部要点是隐藏基础执行程序可能具有的所有非接口方法.

The Delegated... classes (there's a DelegatedExecutorService too) are just passing all the calls to the underlying executor, the ScheduledThreadPoolExecutor in this case. The comments in the code suggest that the whole point of these classes is to hide all the non-interface methods that the underlying executor might have.

无论如何,最好还是使用接口而不是要处理的对象的类版本(List而不是ArrayList,ScheduledExecutorService而不是ScheduledThreadPoolExecutor).

In any case, it is better practice anyway to use the interface rather than the class version of the object you are working on (List and not ArrayList, ScheduledExecutorService and not ScheduledThreadPoolExecutor).

如果您绝对需要ScheduledThreadPoolExecutor中可用的功能,而不是ScheduledExecutorService中可用的功能,则可以使用ScheduledThreadPoolExecutor的构造函数来创建它的实例.

If you absolutely need functionality available in ScheduledThreadPoolExecutor and not in the ScheduledExecutorService, you could use the constructor of ScheduledThreadPoolExecutor to create an instance of it.

更多推荐

使用执行程序创建ScheduledThreadPoolExecutor

本文发布于:2023-11-23 09:54:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   ScheduledThreadPoolExecutor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!