欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

非常適合新手學(xué)生的Java線程池超詳細(xì)分析

 更新時(shí)間:2022年03月21日 11:44:08   作者:摸魚打醬油  
作者是一個(gè)來自河源的大三在校生,以下筆記都是作者自學(xué)之路的一些淺薄經(jīng)驗(yàn),如有錯(cuò)誤請(qǐng)指正,將來會(huì)不斷的完善筆記,幫助更多的Java愛好者入門

線程池的好處

  • 可以實(shí)現(xiàn)線程的復(fù)用,避免重新創(chuàng)建線程和銷毀線程。創(chuàng)建線程和銷毀線程對(duì)CPU的開銷是很大的。
  • 可以限制最大可創(chuàng)建的線程數(shù),可根據(jù)自己的機(jī)器性能動(dòng)態(tài)調(diào)整線程池參數(shù),提高應(yīng)用性能。
  • 提供定時(shí)執(zhí)行、并發(fā)數(shù)控制等功能。
  • 統(tǒng)一管理線程。

創(chuàng)建線程池的五種方式

1:緩存線程池(不推薦)

2:固定容量線程池(不推薦)

3:單個(gè)線程池(不推薦)

4:定時(shí)任務(wù)線程池(不推薦)

5:通過ThreadPoolExecutor構(gòu)造方法創(chuàng)建線程池(阿里巴巴開發(fā)手冊(cè)十分推薦)

前面4種創(chuàng)建線程池的方式都是通過Executors的靜態(tài)方法來創(chuàng)建。

緩存線程池CachedThreadPool

	ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < 10; i++) {
            final int finalI = i;
            executorService.execute(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"<thread->run>"+ finalI);
                }
            });
        }

為什么不推薦使用緩存線程池?

源碼分析

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, 2147483647, 60L, TimeUnit.SECONDS, new SynchronousQueue());
    }
 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler);
    }

通過上面兩個(gè)代碼片段,我們可以看出CachedThreadPool的maximumPoolSize為Integer的最大值2147483647,相當(dāng)于可以無限的創(chuàng)建線程,而創(chuàng)建線程是需要內(nèi)存的,這樣就會(huì)造成內(nèi)存溢出,而且一般的機(jī)器也沒用那么大的內(nèi)存給它創(chuàng)建這么大量的線程。

固定容量線程池FixedThreadPool

newFixedThreadPool(int num),num就是我們要指定的固定線程數(shù)量

	ExecutorService executorService = Executors.newFixedThreadPool(5);

      for (int i = 0; i < 10; i++) {
          final int finalI = i;
          executorService.execute(new Runnable() {
              public void run() {
                  System.out.println(Thread.currentThread().getName()+"<thread->run>"+ finalI);
              }
          });
      }

輸出:

pool-1-thread-5<thread->run>4
pool-1-thread-4<thread->run>3
pool-1-thread-5<thread->run>5
pool-1-thread-3<thread->run>2
pool-1-thread-3<thread->run>8
pool-1-thread-3<thread->run>9
pool-1-thread-2<thread->run>1
pool-1-thread-1<thread->run>0
pool-1-thread-5<thread->run>7
pool-1-thread-4<thread->run>6

可以看出起到了線程的復(fù)用。

為什么FixedThreadPool是固定線程池?

源碼分析

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

通過這個(gè)源碼可以看出,核心線程數(shù)(corePoolSize)和最大線程數(shù)(maximumPoolSize)都為nThreads,因?yàn)橹挥羞@樣,線程池才不會(huì)進(jìn)行擴(kuò)容,線程數(shù)才固定。

單個(gè)線程池SingleThreadExecutor

	ExecutorService executorService = Executors.newSingleThreadExecutor();

      for (int i = 0; i < 10; i++) {
          final int finalI = i;
          executorService.execute(new Runnable() {
              public void run() {
                  System.out.println(Thread.currentThread().getName()+"<thread->run>"+ finalI);
              }
          });

      }

為什么SingleThreadExecutor只含有一個(gè)線程?

源碼分析

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

通過這個(gè)源碼可以看出,核心線程數(shù)(corePoolSize)和最大線程數(shù)(maximumPoolSize)都為1,所以它只含有一個(gè)線程。

定時(shí)任務(wù)線程池ScheduledThreadPool

	  int initDelay=10; //初始化延時(shí)
      int period=1;//初始化延遲過了之后,每秒的延時(shí)

      ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);

      scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
          @Override
          public void run() {
              System.out.println(Thread.currentThread().getName()+"<thread->run>");
          }
      },initDelay,period, TimeUnit.SECONDS);

這段代碼的效果是:程序運(yùn)行之后等10秒,然后輸出第一次結(jié)果,之后每隔1秒輸出一次結(jié)果。

為什么不推薦使用ScheduledThreadPool?

源碼分析

public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, 2147483647, 10L, TimeUnit.MILLISECONDS, new ScheduledThreadPoolExecutor.DelayedWorkQueue());
    }

可以看出ScheduledThreadPool的最大線程數(shù)(maximumPoolSize)為Integer的最大值2147483647,相當(dāng)于可以無限的創(chuàng)建線程,而創(chuàng)建線程是需要內(nèi)存的,這樣就會(huì)造成內(nèi)存溢出,而且一般的機(jī)器也沒用那么大的內(nèi)存給它創(chuàng)建這么大量的線程。

ThreadPoolExecutor創(chuàng)建線程池(十分推薦)

	ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
              2L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5),
              Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

      for (int i = 0; i < 12; i++) {
          final int finalI = i;
          threadPoolExecutor.execute(new Runnable() {
              public void run() {
                  System.out.println(Thread.currentThread().getName()+"<thread->run>"+ finalI);
              }
          });
      }

ThreadPoolExecutor的七個(gè)參數(shù)詳解

	public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        
    }
  • corePoolSize:核心線程數(shù)。這些線程一旦被創(chuàng)建不會(huì)被銷毀,是一直存在的。線程池默認(rèn)是沒有線程的,當(dāng)有任務(wù)到來了,就會(huì)通過ThreadFactory去創(chuàng)建線程,并一直存在。
  • maximumPoolSize:最大線程數(shù)。非核心線程數(shù)=maximumPoolSize-corePoolSize,非核心線程數(shù)其實(shí)就是可擴(kuò)容的線程數(shù),可能會(huì)被銷毀。
  • keepAliveTime:非核心線程的空閑存活時(shí)間。當(dāng)通過擴(kuò)容生成的非核心線程數(shù)在keepAliveTime這個(gè)時(shí)間后還處于空閑狀態(tài),則會(huì)銷毀這些非核心線程。
  • unit:keepAliveTime的時(shí)間單位,例如:秒
  • workQueue:等待區(qū)。當(dāng)來了>corePoolSize的任務(wù)時(shí)會(huì)把任務(wù)存放在workQueue這個(gè)阻塞隊(duì)列中,等待其他線程處理。
  • threadFactory:線程工廠。創(chuàng)建線程的一種方式。
  • handler:拒絕策略。當(dāng)來了>最大線程數(shù)+workQueue的容量則會(huì)執(zhí)行拒絕策略

workQueue

ArrayBlockingQueue:有界阻塞隊(duì)列。隊(duì)列有大小限制,當(dāng)容量超過時(shí)則會(huì)觸發(fā)擴(kuò)容或者拒絕策略。

	public ArrayBlockingQueue(int capacity) {
        this(capacity, false);
    }

LinkedBlockingQueue:無界阻塞隊(duì)列,隊(duì)列無大小限制,可能會(huì)造成內(nèi)存溢出。

	 public LinkedBlockingQueue() {
        this(2147483647);
    }

handler

AbortPolicy:直接拋異常

	public static class AbortPolicy implements RejectedExecutionHandler {
        public AbortPolicy() {
        }

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString());
        }
    }

DiscardPolicy:不作任何操作。默默丟棄任務(wù)

	public static class DiscardPolicy implements RejectedExecutionHandler {
        public DiscardPolicy() {
        }

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
        }
    }

DiscardOldestPolicy:丟掉存在時(shí)間最長的任務(wù)

	public static class DiscardOldestPolicy implements RejectedExecutionHandler {
        public DiscardOldestPolicy() {
        }

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                e.getQueue().poll();
                e.execute(r);
            }

        }
    }

CallerRunsPolicy:讓提交任務(wù)的線程去處理任務(wù)

	public static class CallerRunsPolicy implements RejectedExecutionHandler {
        public CallerRunsPolicy() {
        }

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                r.run();
            }

        }
    }

threadFactory

	ThreadFactory threadFactory = Executors.defaultThreadFactory();

      threadFactory.newThread(new Runnable() {
          @Override
          public void run() {
              System.out.println("threadFactory");
          }
      }).start();

如何觸發(fā)拒絕策略和線程池?cái)U(kuò)容?

	ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
              2L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5),
              Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

      for (int i = 0; i < 26; i++) { //并發(fā)數(shù)26
          final int finalI = i;
          threadPoolExecutor.execute(new Runnable() {
              public void run() {
                  System.out.println(Thread.currentThread().getName()+"<thread->run>"+ finalI);
              }
          });
      }
      /**
       * 核心線程數(shù)=10,最大線程數(shù)=20,故可擴(kuò)容線程數(shù)=20-10
       * BlockingQueue的大小為5,故等待區(qū)的大小為5,也就是當(dāng)并發(fā)數(shù)<=核心線程數(shù)+5不會(huì)擴(kuò)容,并發(fā)數(shù)大于16才會(huì)擴(kuò)容
       *
       * 觸發(fā)擴(kuò)容:并發(fā)數(shù)>核心線程數(shù)+阻塞隊(duì)列的大小
       * 對(duì)于這段代碼,如果來了26個(gè)并發(fā),10個(gè)并發(fā)會(huì)被核心線程處理,5個(gè)會(huì)在等待區(qū),剩下11個(gè)會(huì)因?yàn)榈却齾^(qū)滿了而觸發(fā)擴(kuò)容
       * 因?yàn)檫@里最多能夠擴(kuò)容10個(gè),這里卻是11個(gè),所以會(huì)觸發(fā)拒絕策略
       */
  • 為什么這段代碼會(huì)觸發(fā)拒絕策略

對(duì)于這段代碼,如果來了26個(gè)并發(fā),10個(gè)并發(fā)會(huì)被核心線程處理,5個(gè)會(huì)在等待區(qū),剩下11個(gè)會(huì)因?yàn)榈却齾^(qū)滿了而觸發(fā)擴(kuò)容,但是又因?yàn)橐驗(yàn)檫@里最多能夠擴(kuò)容10個(gè),這里卻是11個(gè),所以會(huì)觸發(fā)拒絕策略。

  • 怎么觸發(fā)擴(kuò)容

觸發(fā)擴(kuò)容:并發(fā)數(shù)>核心線程數(shù)(corePoolSize)+阻塞隊(duì)列(workQueue)的大小

  • 使用Java純手寫一個(gè)線程池

下期文章鏈接http://www.dbjr.com.cn/article/241589.htm

到此這篇關(guān)于非常適合新手學(xué)生的Java線程池超詳細(xì)分析的文章就介紹到這了,更多相關(guān)Java 線程池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Core Java 簡單談?wù)凥ashSet(推薦)

    Core Java 簡單談?wù)凥ashSet(推薦)

    下面小編就為大家?guī)硪黄狢ore Java 簡單談?wù)凥ashSet(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • java樹形菜單對(duì)象生成

    java樹形菜單對(duì)象生成

    這篇文章主要為大家詳細(xì)介紹了java樹形菜單對(duì)象生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Java對(duì)稱加密算法DES實(shí)例詳解

    Java對(duì)稱加密算法DES實(shí)例詳解

    這篇文章主要介紹了Java對(duì)稱加密算法DES,結(jié)合實(shí)例形式詳細(xì)分析了java DES算法的概念、原理、實(shí)現(xiàn)方法與應(yīng)用場(chǎng)景,需要的朋友可以參考下
    2019-09-09
  • 解決SpringMVC Controller 接收頁面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題

    解決SpringMVC Controller 接收頁面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題

    下面小編就為大家分享一篇解決SpringMVC Controller 接收頁面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • SpringBoot整合ES解析搜索返回字段問題

    SpringBoot整合ES解析搜索返回字段問題

    這篇文章主要介紹了SpringBoot整合ES解析搜索返回字段問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • spring boot輸入數(shù)據(jù)校驗(yàn)(validation)的實(shí)現(xiàn)過程

    spring boot輸入數(shù)據(jù)校驗(yàn)(validation)的實(shí)現(xiàn)過程

    web項(xiàng)目中,用戶的輸入總是被假定不安全不正確的,在被處理前需要做校驗(yàn)。本文介紹在spring boot項(xiàng)目中實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的過程,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-09-09
  • Java中的FileInputStream是否需要close問題

    Java中的FileInputStream是否需要close問題

    這篇文章主要介紹了Java中的FileInputStream是否需要close問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 線程池調(diào)用kafka發(fā)送消息產(chǎn)生的內(nèi)存泄漏問題排查解決

    線程池調(diào)用kafka發(fā)送消息產(chǎn)生的內(nèi)存泄漏問題排查解決

    這篇文章主要為大家介紹了線程池調(diào)用kafka發(fā)送消息產(chǎn)生的內(nèi)存泄漏問題排查解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Spring boot配置 swagger的示例代碼

    Spring boot配置 swagger的示例代碼

    Swagger是一組開源項(xiàng)目,Spring 基于swagger規(guī)范,可以將基于SpringMVC和Spring Boot項(xiàng)目的項(xiàng)目代碼,自動(dòng)生成JSON格式的描述文件,接下來通過本文給大家介紹Spring boot配置 swagger的示例代碼,一起看看吧
    2021-09-09
  • 詳解springboot和vue前后端分離開發(fā)跨域登陸問題

    詳解springboot和vue前后端分離開發(fā)跨域登陸問題

    這篇文章主要介紹了詳解springboot和vue前后端分離開發(fā)跨域登陸問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評(píng)論