亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

java并發(fā)編程學習之線程池-預定義線程池(四)

suemi / 1078人閱讀

摘要:系統(tǒng)預定了幾個線程池,不過建議手動創(chuàng)建,以防止錯誤創(chuàng)建消耗資源,比如創(chuàng)建太多線程或者固定線程數(shù)量,無界隊列固定線程數(shù)量,數(shù)量為,無界隊列,會按順序執(zhí)行不限制線程數(shù)量,使用隊列,使用于短任務基于用于周期性執(zhí)行任務示例第一個是,第二個是第一

系統(tǒng)預定了幾個線程池,不過建議手動創(chuàng)建,以防止錯誤創(chuàng)建消耗資源,比如創(chuàng)建太多線程或者OOM

FixedThreadPool

固定線程數(shù)量,無界隊列

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue());
}
SingleThreadExecutor

固定線程數(shù)量,數(shù)量為1,無界隊列,會按順序執(zhí)行

public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue()));
}
CachedThreadPool

不限制線程數(shù)量,使用SynchronousQueue隊列,使用于短任務

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue());
}
WorkStealingPool

基于ForkJoinPool

public static ExecutorService newWorkStealingPool(int parallelism) {
    return new ForkJoinPool
        (parallelism,
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
}
ScheduledThreadPoolExecutor

用于周期性執(zhí)行任務

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
}
示例
public class ScheduledDemo {
    static class Thread1 implements Runnable {
        @Override
        public void run() {
            SimpleDateFormat formater = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            System.out.println(Thread.currentThread().getName() + ":" + formater.format(new Date()));
        }
    }

    public static void main(String[] args) {
        ScheduledThreadPoolExecutor schedule
                = new ScheduledThreadPoolExecutor(1);
        //第一個是Runnable,第二個是第一次開始的時間,第三個是周期時間,第四個是時間單位
        schedule.scheduleAtFixedRate(new Thread1(),1000,1000, TimeUnit.MILLISECONDS);
    }
}

運行結(jié)果如下:

文章版權歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/75723.html

相關文章

  • java并發(fā)編程習之線程-ThreadPoolExecutor(三)

    摘要:是所有線程池實現(xiàn)的父類,我們先看看構(gòu)造函數(shù)構(gòu)造參數(shù)線程核心數(shù)最大線程數(shù)線程空閑后,存活的時間,只有線程數(shù)大于的時候生效存活時間的單位任務的阻塞隊列創(chuàng)建線程的工程,給線程起名字當線程池滿了,選擇新加入的任務應該使用什么策略,比如拋異常丟棄當前 ThreadPoolExecutor ThreadPoolExecutor是所有線程池實現(xiàn)的父類,我們先看看構(gòu)造函數(shù) 構(gòu)造參數(shù) corePool...

    阿羅 評論0 收藏0
  • java并發(fā)編程習之線程-Executor和ExecutorService(一)

    摘要:接口用于提交任務接口繼承了接口設置線程的狀態(tài),還沒執(zhí)行的線程會被中斷設置線程的狀態(tài),嘗試停止正在進行的線程當調(diào)用或方法后返回為當調(diào)用方法后,并且所有提交的任務完成后返回為當調(diào)用方法后,成功停止后返回為當前線程阻塞,直到線程執(zhí)行完時間到被中斷 Executor接口 void execute(Runnable command)//用于提交command任務 ExecutorService接...

    liuchengxu 評論0 收藏0
  • java并發(fā)編程習之線程-AbstractExecutorService(二)

    摘要:抽象類,實現(xiàn)了的接口。將任務封裝成提交任務主要方法在任務是否超時超時時間任務書用于存放結(jié)果的,先完成的放前面。 AbstractExecutorService抽象類,實現(xiàn)了ExecutorService的接口。 newTaskFor 將任務封裝成FutureTask protected RunnableFuture newTaskFor(Runnable runnable, T va...

    Jokcy 評論0 收藏0
  • java并發(fā)編程習之線程的生命周期-join(

    摘要:定義等待該線程終止,比如線程調(diào)用了線程的,那么線程要等到線程執(zhí)行完后,才可以繼續(xù)執(zhí)行。 定義 等待該線程終止,比如A線程調(diào)用了B線程的join,那么A線程要等到B線程執(zhí)行完后,才可以繼續(xù)執(zhí)行。 示例 public class JoinDemo { static class JoinThread1 implements Runnable { Thread thre...

    xavier 評論0 收藏0
  • 一起并發(fā)編程 - 簡易線程實現(xiàn)

    摘要:并且,線程池在某些情況下還能動態(tài)調(diào)整工作線程的數(shù)量,以平衡資源消耗和工作效率。同時線程池還提供了對池中工作線程進行統(tǒng)一的管理的相關方法。 開發(fā)中經(jīng)常會遇到各種池(如:連接池,線程池),它們的作用就是為了提高性能及減少開銷,在JDK1.5以后的java.util.concurrent包中內(nèi)置了很多不同使用場景的線程池,為了更好的理解它們,自己手寫一個線程池,加深印象。 概述 1.什么是...

    Harriet666 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<