av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語言

java線程池框架是什么

時(shí)間:2025-01-11 15:27:26 java語言 我要投稿
  • 相關(guān)推薦

java線程池框架是什么

  多線程是程序員面試時(shí)常常會面對的問題,對多線程概念的掌握和理解水平,也常常被用來衡量一個(gè)人的編程實(shí)力。不錯,普通的多線程已經(jīng)不容易了。以下是小編為大家搜索整理java線程池框架是什么,希望能給大家?guī)韼椭?更多精彩內(nèi)容請及時(shí)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  一、線程池結(jié)構(gòu)圖

  二、示例

  定義線程接口

  6public class MyThread extends Thread {@Overridepublicvoid run() {System.out.println(Thread.currentThread().getName() + "正在執(zhí)行");}}

  1:newSingleThreadExecutor

  10ExecutorService pool = Executors. newSingleThreadExecutor();Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();//將線程放入池中進(jìn)行執(zhí)行pool.execute(t1);pool.execute(t2);pool.execute(t3);//關(guān)閉線程池pool.shutdown();

  輸入結(jié)果:

  3pool-1-thread-1正在執(zhí)行pool-1-thread-1正在執(zhí)行pool-1-thread-1正在執(zhí)行

  2:newFixedThreadPool

  13ExecutorService pool = Executors.newFixedThreadPool(3);Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();Thread t4 = new MyThread();Thread t5 = new MyThread();//將線程放入池中進(jìn)行執(zhí)行pool.execute(t1);pool.execute(t2);pool.execute(t3);pool.execute(t4);pool.execute(t5);pool.shutdown();

  輸入結(jié)果:

  4pool-1-thread-1正在執(zhí)行pool-1-thread-2正在執(zhí)行pool-1-thread-1正在執(zhí)行pool-1-thread-2正在執(zhí)行

  3 :newCachedThreadPool

  14ExecutorService pool = Executors.newCachedThreadPool();Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();Thread t4 = new MyThread();Thread t5 = new MyThread();//將線程放入池中進(jìn)行執(zhí)行pool.execute(t1);pool.execute(t2);pool.execute(t3);pool.execute(t4);pool.execute(t5);//關(guān)閉線程池pool.shutdown();

  輸入結(jié)果:

  5pool-1-thread-2正在執(zhí)行pool-1-thread-4正在執(zhí)行pool-1-thread-3正在執(zhí)行pool-1-thread-1正在執(zhí)行pool-1-thread-5正在執(zhí)行

  4 :ScheduledThreadPoolExecutor

  14ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);pool.scheduleAtFixedRate(new Runnable() {//每隔一段時(shí)間就觸發(fā)異常 @Override public void run() { //throw new RuntimeException(); System.out.println("================"); }}, 1000, 2000, TimeUnit.MILLISECONDS);pool.scheduleAtFixedRate(new Runnable() {//每隔一段時(shí)間打印系統(tǒng)時(shí)間,證明兩者是互不影響的 @Override public void run() { System.out.println("+++++++++++++++++"); }}, 1000, 2000, TimeUnit.MILLISECONDS);

  輸入結(jié)果:

  4================+++++++++++++++++++++++++++++++++++++++++++++++++++

  三、線程池核心參數(shù)

  corePoolSize : 池中核心的線程數(shù)

  maximumPoolSize : 池中允許的最大線程數(shù)。

  keepAliveTime : 當(dāng)線程數(shù)大于核心時(shí),此為終止前多余的空閑線程等待新任務(wù)的最長時(shí)間。

  unit : keepAliveTime 參數(shù)的時(shí)間單位。

  workQueue : 執(zhí)行前用于保持任務(wù)的隊(duì)列。此隊(duì)列僅保持由 execute方法提交的 Runnable任務(wù)。

  threadFactory : 執(zhí)行程序創(chuàng)建新線程時(shí)使用的工廠。

  handler : 由于超出線程范圍和隊(duì)列容量而使執(zhí)行被阻塞時(shí)所使用的處理程序。

  ThreadPoolExecutor :Executors類的底層實(shí)現(xiàn)。

  3.1 任務(wù)排隊(duì)機(jī)制

  SynchonousQueue: 同步隊(duì)列,隊(duì)列直接提交給線程執(zhí)行而不保持它們,此時(shí)線程池通常是無界的

  LinkedBlockingQueue: 無界對列,當(dāng)線程池線程數(shù)達(dá)到最大數(shù)量時(shí),新任務(wù)就會在隊(duì)列中等待執(zhí)行,可能會造成隊(duì)列無限膨脹

  ArrayBlockingQueue : 有界隊(duì)列,有助于防止資源耗盡,一旦達(dá)到上限,可能會造成新任務(wù)丟失

  注意:

  newSingleThreadExecutor、newFixedThreadPool使用的是LinkedBlockingQueue

  newCachedThreadPool 使用的是 SynchonousQueue

  newScheduledThreadPool使用的是 DelayedWorkQueue

  3.2 線程執(zhí)行流程

  3.3 線程大小確定:

  cpu密集型: 盡量少開線程,最佳線程數(shù) Ncpu+1

  io密集型:多開線程,2Ncpu

  混合型:根據(jù)情況而定,可以拆分成io密集和cou密集

【java線程池框架是什么】相關(guān)文章:

什么是java線程池框架10-02

java線程池框架解析09-11

java線程池框架解析方法10-13

Java 5線程池使用07-28

java中通用的線程池實(shí)例代碼08-27

java主線程是什么05-20

java的多線程09-09

java多線程08-31

Java多線程的線程守護(hù)例子06-08