Reputation: 1760
When I create the ExecutorService, I use one of the factories to create threads, for example, we have 3 threads:
Executors.newScheduledThreadPool (3)
What happens with resources and memory, when we use the factory? These threads already exist or they create when I start the tasks? What does it mean to create a thread (native code)?
Upvotes: 0
Views: 118
Reputation: 21883
It will create and return a ScheduledThreadPoolExecutor with the following characteristics
This class specializes ThreadPoolExecutor implementation by
1. Using a custom task type, ScheduledFutureTask for
tasks, even those that don't require scheduling (i.e.,
those submitted using ExecutorService execute, not
ScheduledExecutorService methods) which are treated as
delayed tasks with a delay of zero.
2. Using a custom queue (DelayedWorkQueue) based on an
unbounded DelayQueue. The lack of capacity constraint and
the fact that corePoolSize and maximumPoolSize are
effectively identical simplifies some execution mechanics
(see delayedExecute) compared to ThreadPoolExecutor
version.
The DelayedWorkQueue class is defined below for the sake of
ensuring that all elements are instances of
RunnableScheduledFuture. Since DelayQueue otherwise
requires type be Delayed, but not necessarily Runnable, and
the workQueue requires the opposite, we need to explicitly
define a class that requires both to ensure that users don't
add objects that aren't RunnableScheduledFutures via
getQueue().add() etc.
3. Supporting optional run-after-shutdown parameters, which
leads to overrides of shutdown methods to remove and cancel
tasks that should NOT be run after shutdown, as well as
different recheck logic when task (re)submission overlaps
with a shutdown.
4. Task decoration methods to allow interception and
instrumentation, which are needed because subclasses cannot
otherwise override submit methods to get this effect. These
don't have any impact on pool control logic though.
For your question
These threads already exist or they create when I start the tasks?
There will be Threads created and pooled for the size of corePoolSize
which is 3 in this case.
Upvotes: 2