2016-11-11 13 views
0

當我創建了ExecutorService的,我用的是工廠創建一個線程,例如,我們有3個主題:用Executors.newScheduledThreadPool(n)創建線程是什麼意思?

Executors.newScheduledThreadPool (3) 

與資源和內存會發生什麼,當我們使用工廠?這些線程已經存在,或者當我開始執行任務時創建它們?創建線程(本機代碼)是什麼意思?

+0

這是什麼意思「流」在這裏? –

+0

@NicolasFilotto他的意思是「線索」。 –

+0

是的,我的意思是線程! – Delphian

回答

2

它將創建並具有以下特點

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. 

對於你的問題返回ScheduledThreadPoolExecutor

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

有將線程創建併合並用於corePoolSize其尺寸爲3在這種情況下, 。

+0

這意味着只要我不使用這些線程,CPU時間將被用於什麼都沒有?線程的基礎是什麼,是一個無限循環?感謝您的鏈接和答案! – Delphian

+0

閱讀ThreadPoolExecutor的文檔https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html – shazin