缓存线程池如何重用现有线程 [英] How Does a Cached Thread Pool Reuse Existing Threads
问题描述
我刚刚开始查看Java的 Executors
类和 newCachedThreadPool()
方法。根据API,生成的线程池重用现有的 Thread
对象来执行新任务。
I've just started looking at Java's Executors
class and the newCachedThreadPool( )
method. According to the API, the resulting thread pool reuses existing Thread
objects for new tasks.
我有点很困惑这是如何实现的,因为我找不到 Thread
API中的任何方法,它允许你设置现有 Thread $ c的行为$ c> object。
I'm a bit puzzled how this is implemented because I couldn't find any method in the Thread
API that lets you set the behaviour of an existing Thread
object.
例如,您可以从以下位置创建 new Thread
一个 Runnable
对象,它使 Thread
调用 Runnable
的 run()
方法。但是, Thread
API中没有setter方法,它将 Runnable
作为参数。
For example, you can create a new Thread
from a Runnable
object, which makes the Thread
call the Runnable
's run( )
method. However, there is no setter method in the Thread
API that takes a Runnable
as an argument.
我很感激任何指针。
推荐答案
执行官在后台为你做一切。是的,它仅使用现有的线程API。
Executors does everything for you in the background. And yes it uses the existing thread API only.
下面的链接包含使用Thread类和Collection API实现的线程池的示例实现: http://www.ibm.com/developerworks/library/j-jtp0730/index.html
Below link has sample implementation of Thread pool implemented using Thread class and Collection API: http://www.ibm.com/developerworks/library/j-jtp0730/index.html
这篇关于缓存线程池如何重用现有线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!