Java 線(xiàn)程

線(xiàn)程

<code>java<code> 創(chuàng)建線(xiàn)程最簡(jiǎn)單的方式就是繼承<code>Thread</code>類(lèi)或者實(shí)現(xiàn) <code>Runnable</code>接口實(shí)現(xiàn) <code>run()</code> 方法,然后調(diào)用 <code>start()</code> 方法來(lái)讓線(xiàn)程執(zhí)行。

public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread());
            }
        }).start();
    }

輸出:

Thread[Thread-0,5,main] 這個(gè)是輸出的是線(xiàn)程名,線(xiàn)程優(yōu)先級(jí)以及線(xiàn)程組的名字,具體可以看<code>Thread</code> 的<code>toString()</code> 方法

實(shí)際上 實(shí)現(xiàn) Runnable 接口的類(lèi)可以看做是一個(gè)任務(wù)(<code>task</code>)真正創(chuàng)建線(xiàn)程的是 <code>Thread</code>類(lèi)

線(xiàn)程池

JDK 中提供了 Executor 框架來(lái)創(chuàng)建線(xiàn)程池。
其中 <code>Executors</code>作為工廠(chǎng)類(lèi) 提供了幾個(gè)創(chuàng)建線(xiàn)程的工廠(chǎng)方法

public class ThreadTest {

    public static void main(String[] args) {
        TestTask tt = new TestTask();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i <10;i++){
            executorService.submit(tt);
        }
    }

}

class TestTask implements Runnable {

    @Override
    public void run() {
        System.out.println(System.currentTimeMillis() + " Thread ID:" + Thread.currentThread().getId());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

輸出:

1495507701904 Thread ID:10
1495507701905 Thread ID:11
1495507701904 Thread ID:9
1495507701905 Thread ID:12
1495507701905 Thread ID:13
1495507702909 Thread ID:9
1495507702909 Thread ID:12
1495507702909 Thread ID:11
1495507702909 Thread ID:13
1495507702909 Thread ID:10

<code>newFixedThreadPool(int nThreads)</code> 接收一個(gè) <code>int</code> 參數(shù),返回一個(gè)固定大小的線(xiàn)程池,上面代碼的輸出可以看出十個(gè)任務(wù)提交到線(xiàn)程池中,前5個(gè)和后面的5個(gè)執(zhí)行相差了1秒。
如果換成<code>newCachedThreadPool()</code>來(lái)創(chuàng)建線(xiàn)程池則會(huì)得到如下輸出:

1495508104585 Thread ID:9
1495508104586 Thread ID:10
1495508104586 Thread ID:11
1495508104586 Thread ID:12
1495508104586 Thread ID:13
1495508104586 Thread ID:14
1495508104587 Thread ID:15
1495508104587 Thread ID:16
1495508104587 Thread ID:17
1495508104587 Thread ID:18

線(xiàn)程池的核心

<code>Executors</code> 提供這些工廠(chǎng)方法都是通過(guò)<code>ThreadPoolExecutor</code>來(lái)實(shí)現(xiàn)
<code>ThreadPoolExecutor</code>中提供了四個(gè)構(gòu)造方法,而其他三個(gè)都是通過(guò)下圖這個(gè)構(gòu)造方法來(lái)實(shí)現(xiàn)。


jdk 源碼上提供的注釋說(shuō)明

/**
    * Creates a new {@code ThreadPoolExecutor} with the given initial
    * parameters.
    *
    * @param corePoolSize the number of threads to keep in the pool, even
    *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
    * @param maximumPoolSize the maximum number of threads to allow in the
    *        pool
    * @param keepAliveTime when the number of threads is greater than
    *        the core, this is the maximum time that excess idle threads
    *        will wait for new tasks before terminating.
    * @param unit the time unit for the {@code keepAliveTime} argument
    * @param workQueue the queue to use for holding tasks before they are
    *        executed.  This queue will hold only the {@code Runnable}
    *        tasks submitted by the {@code execute} method.
    * @param threadFactory the factory to use when the executor
    *        creates a new thread
    * @param handler the handler to use when execution is blocked
    *        because the thread bounds and queue capacities are reached
    * @throws IllegalArgumentException if one of the following holds:<br>
    *         {@code corePoolSize < 0}<br>
    *         {@code keepAliveTime < 0}<br>
    *         {@code maximumPoolSize <= 0}<br>
    *         {@code maximumPoolSize < corePoolSize}
    * @throws NullPointerException if {@code workQueue}
    *         or {@code threadFactory} or {@code handler} is null
    */

<code> corePoolSize </code> 指定線(xiàn)程池中保持的線(xiàn)程數(shù)量,即使他們是空閑的,除非設(shè)置了<code>allowCoreThreadTimeOut</code>

<code> maximumPoolSize </code> 指定線(xiàn)程池允許創(chuàng)建的最大線(xiàn)程數(shù)量

<code> keepAliveTime </code> 當(dāng)線(xiàn)程池中的線(xiàn)程數(shù)量大于<code> corePoolSize </code>設(shè)置的數(shù)量時(shí),大于<code> corePoolSize </code>數(shù)量的那部分線(xiàn)程等待新任務(wù)的最大等待時(shí)間,當(dāng)超過(guò)這個(gè)時(shí)間多出來(lái)的線(xiàn)程將會(huì)被銷(xiāo)毀。

<code> unit </code> <code> keepAliveTime </code>的時(shí)間單位

<code> workQueue </code> 用于保存通過(guò) <code>execute</code> 方法提交并且未來(lái)得及執(zhí)行的任務(wù)。

<code> threadFactory </code> 用來(lái)創(chuàng)建線(xiàn)程的線(xiàn)程工廠(chǎng)
<code> handler </code> 當(dāng)線(xiàn)程池中線(xiàn)程數(shù)量到達(dá)極限并且 workQueue達(dá)到最大容量時(shí) 處理任務(wù)的拒絕策略

workQueue傳入的是一個(gè) <code>BlockingQueue</code>類(lèi)型的參數(shù),用于保存 Runnable 對(duì)象

可使用的<code>BlockingQueue</code>有:
<code>SynchronousQueue</code> 直接提交的隊(duì)列
<code>ArrayBlockingQueue</code> 有界隊(duì)列
<code>LinkedBlockingQueue</code> 無(wú)界隊(duì)列
<code>PriorityBlockingQueue</code> 優(yōu)先級(jí)隊(duì)列

<code>handler</code>
有四種拒絕策略
<code>AbortPolicy</code> 直接拋出異常
<code>DiscardPolicy</code> 默默拋棄
<code>DiscardOldestPolicy</code> 丟棄最老的任務(wù),也就是即將執(zhí)行的任務(wù)
<code>CallerRunsPolicy</code> 在調(diào)用線(xiàn)程中執(zhí)行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容