国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

JAVA 線程池的使用

2019-11-14 11:31:53
字體:
供稿:網(wǎng)友

java通過Executors提供四種線程池,分別為:newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。newFixedThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線程池,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待。newScheduledThreadPool 創(chuàng)建一個(gè)定長(zhǎng)線程池,支持定時(shí)及周期性任務(wù)執(zhí)行。newSingleThreadExecutor 創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行。

 

(1) newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長(zhǎng)度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。示例代碼如下:

package test;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  public class ThreadPoolExecutorTest {   public static void main(String[] args) {    ExecutorService cachedThreadPool = Executors.newCachedThreadPool();    for (int i = 0; i < 10; i++) {     final int index = i;     try {      Thread.sleep(index * 1000);     } catch (InterruptedException e) {      e.PRintStackTrace();     }     cachedThreadPool.execute(new Runnable() {      public void run() {       System.out.println(index);      }     });    }   }  }  

 

線程池為無限大,當(dāng)執(zhí)行第二個(gè)任務(wù)時(shí)第一個(gè)任務(wù)已經(jīng)完成,會(huì)復(fù)用執(zhí)行第一個(gè)任務(wù)的線程,而不用每次新建線程。 (2) newFixedThreadPool創(chuàng)建一個(gè)定長(zhǎng)線程池,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待。示例代碼如下:

package test;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  public class ThreadPoolExecutorTest {   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(2000);       } catch (InterruptedException e) {        e.printStackTrace();       }      }     });    }   }  }  

 因?yàn)榫€程池大小為3,每個(gè)任務(wù)輸出index后sleep 2秒,所以每?jī)擅氪蛴?個(gè)數(shù)字。定長(zhǎng)線程池的大小最好根據(jù)系統(tǒng)資源進(jìn)行設(shè)置。如Runtime.getRuntime().availableProcessors()

 

(3)  newScheduledThreadPool創(chuàng)建一個(gè)定長(zhǎng)線程池,支持定時(shí)及周期性任務(wù)執(zhí)行。延遲執(zhí)行示例代碼如下:

package test;  import java.util.concurrent.Executors;  import java.util.concurrent.ScheduledExecutorService;  import java.util.concurrent.TimeUnit;  public class ThreadPoolExecutorTest {   public static void main(String[] args) {    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);    scheduledThreadPool.schedule(new Runnable() {     public void run() {      System.out.println("delay 3 seconds");     }    }, 3, TimeUnit.SECONDS);   }  }  

 表示延遲3秒執(zhí)行。

定期執(zhí)行示例代碼如下:

package test;  import java.util.concurrent.Executors;  import java.util.concurrent.ScheduledExecutorService;  import java.util.concurrent.TimeUnit;  public class ThreadPoolExecutorTest {   public static void main(String[] args) {    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);    scheduledThreadPool.scheduleAtFixedRate(new Runnable() {     public void run() {      System.out.println("delay 1 seconds, and excute every 3 seconds");     }    }, 1, 3, TimeUnit.SECONDS);   }  }  

 表示延遲1秒后每3秒執(zhí)行一次。

 

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

package test;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  public class ThreadPoolExecutorTest {   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() {       try {        System.out.println(index);        Thread.sleep(2000);       } catch (InterruptedException e) {        e.printStackTrace();       }      }     });    }   }  }  

 結(jié)果依次輸出,相當(dāng)于順序執(zhí)行各個(gè)任務(wù)。

你可以使用JDK自帶的監(jiān)控工具來監(jiān)控我們創(chuàng)建的線程數(shù)量,運(yùn)行一個(gè)不終止的線程,創(chuàng)建指定量的線程,來觀察:工具目錄:C:/Program Files/Java/jdk1.6.0_06/bin/jconsole.exe運(yùn)行程序做稍微修改:

package test;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  public class ThreadPoolExecutorTest {   public static void main(String[] args) {    ExecutorService singleThreadExecutor = Executors.newCachedThreadPool();    for (int i = 0; i < 100; i++) {     final int index = i;     singleThreadExecutor.execute(new Runnable() {      public void run() {       try {        while(true) {         System.out.println(index);         Thread.sleep(10 * 1000);        }       } catch (InterruptedException e) {        e.printStackTrace();       }      }     });     try {      Thread.sleep(500);     } catch (InterruptedException e) {      e.printStackTrace();     }    }   }  }  

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌邑市| 繁昌县| 浠水县| 揭西县| 陕西省| 青田县| 青河县| 琼海市| 勃利县| 中山市| 武陟县| 木里| 苏尼特右旗| 邵东县| 广河县| 洪雅县| 鸡西市| 溧水县| 望江县| 龙口市| 五常市| 武山县| 合江县| 牡丹江市| 广河县| 赣榆县| 古交市| 绩溪县| 全南县| 洞头县| 大丰市| 沙雅县| 揭阳市| 绥中县| 台南市| 平顶山市| 民勤县| 兴海县| 翼城县| 鄱阳县| 监利县|