2016-11-11 2 views
0

Когда я создаю ExecutorService, я использую один из заводов для создания потоков, например, у нас есть 3 темы:Что значит создать поток с помощью Executors.newScheduledThreadPool (n)?

Executors.newScheduledThreadPool (3) 

Что происходит с ресурсами и памятью, когда мы используем завод? Эти потоки уже существуют или создаются при запуске задач? Что значит создать поток (собственный код)?

+0

Что вы подразумеваете под "потоком"? –

+0

@NicolasFilotto Он означает «потоки». –

+0

Да, я имею в виду темы! – Delphian

ответ

2

Он будет создавать и возвращать ScheduledThreadPoolExecutor со следующими характеристиками

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. 

Для вашего вопроса

These threads already exist or they create when I start the tasks? 

Там будут Темы созданы и объединены для размера corePoolSize, который 3 в этом случае ,

+0

Это означает, что время процессора будет использоваться для ничего, пока я не использую эти потоки? Что лежит в основе потока, бесконечный цикл? Спасибо за ссылки и ответ! – Delphian

+0

Прочитайте документацию для ThreadPoolExecutor https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html. – shazin

Смежные вопросы