java創(chuàng)建線程池有幾種方式

這篇文章將為大家詳細(xì)講解有關(guān)java創(chuàng)建線程池有幾種方式,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)公司主營黃石港網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶APP開發(fā)公司,黃石港h5重慶小程序開發(fā)搭建,黃石港網(wǎng)站營銷推廣歡迎黃石港等地區(qū)企業(yè)咨詢

java創(chuàng)建線程池的四種方式是:1、newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池;2、newFixedThreadPool創(chuàng)建一個(gè)定長線程池;3、newScheduledThreadPool創(chuàng)建一個(gè)定長線程池。

java創(chuàng)建線程池有幾種方式

Java通過Executors提供四種線程池,分別為:
newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。

newFixedThreadPool 創(chuàng)建一個(gè)定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待。

newScheduledThreadPool 創(chuàng)建一個(gè)定長線程池,支持定時(shí)及周期性任務(wù)執(zhí)行。

newSingleThreadExecutor 創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。

詳細(xì)內(nèi)容可參見博友的博客Java并發(fā)編程:線程池的使用

1.newCachedThreadPool 這里的線程池是無限大的,當(dāng)一個(gè)線程完成任務(wù)之后,這個(gè)線程可以接下來完成將要分配的任務(wù),而不是創(chuàng)建一個(gè)新的線程。

public static void main(String[] args) {  
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();  
        for (int i = 0; i < 10; i++) {  
            final int index = i;  
            try {  
                Thread.sleep(10);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
            cachedThreadPool.execute(new Runnable() {  
                public void run() {  
                    System.out.println(index);  
                }  
            });  
        }  
    }

2.newFixedThreadPool

public static void main(String[] args) {  
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);  
        for (int i = 0; i < 10; i++) {  
            final int index = i;  
            fixedThreadPool.execute(new Runnable() {  
                public void run() {  
                    try {  
                        System.out.println(index);  
                        Thread.sleep(10);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
            });  
        }  
    }

3.newScheduledThreadPoo

public static void main(String[] args) {  
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);  
        for (int i = 0; i < 10; i++) {  
            scheduledThreadPool.schedule(new Runnable() {  
                public void run() {  
                    System.out.println("delay 3 seconds");  
                }  
            }, 3, TimeUnit.SECONDS);  
        }  
  
    }

4.newSingleThreadExecutor 按順序來執(zhí)行線程任務(wù)   但是不同于單線程,這個(gè)線程池只是只能存在一個(gè)線程,這個(gè)線程死后另外一個(gè)線程會(huì)補(bǔ)上

public static void main(String[] args) {  
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();  
        for (int i = 0; i < 10; i++) {  
            final int index = i;  
            singleThreadExecutor.execute(new Runnable() {  
                public void run() {  
/*                  System.out.println(index);*/  
                    try {  
                        System.out.println(index);  
                        Thread.sleep(2000);  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                }  
            });  
        }  
    }

關(guān)于java創(chuàng)建線程池有幾種方式就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

分享題目:java創(chuàng)建線程池有幾種方式
瀏覽路徑:http://bm7419.com/article44/jjccee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、Google品牌網(wǎng)站設(shè)計(jì)、電子商務(wù)、定制網(wǎng)站、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)