一、創(chuàng)建線程池的6種方式:
Executors.newCachedThreadPool();
創(chuàng)建一個(gè)可緩存線程池踏施,應(yīng)用中存在的線程數(shù)可以無限大Executors.newFixedThreadPool(2)
創(chuàng)建一個(gè)定長線程池应又,可控制線程最大并發(fā)數(shù)宙项,超出的線程會(huì)在隊(duì)列中等待。Executors.newScheduledThreadPool(2)
創(chuàng)建一個(gè)定長線程池株扛,支持定時(shí)及周期性任務(wù)執(zhí)行尤筐。
4 Executors.newSingleThreadExecutor();
創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù)洞就,
保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行盆繁。
5 Executors.newSingleThreadScheduledExecutor();
創(chuàng)建一個(gè)單例線程池,定期或延時(shí)執(zhí)行任務(wù)旬蟋。
6 Executors.newWorkStealingPool(3);
創(chuàng)建一個(gè)帶并行級(jí)別的線程池油昂,并行級(jí)別決定了同一時(shí)刻最多有多少個(gè)線程在執(zhí)行,如不穿如并行級(jí)別參數(shù)倾贰,將默認(rèn)為當(dāng)前系統(tǒng)的CPU個(gè)數(shù)冕碟。
二、代碼
線程代碼
public class ThreadForpools implements Runnable {
private Integer index;
public ThreadForpools(Integer index) {
this.index = index;
}
@Override
public void run() {
/***
* 業(yè)務(wù)......省略
*/
try {
System.out.println(index + " 開始處理線程4艺恪0菜隆!");
Thread.sleep(index * 2000); //等2秒方便參觀
System.out.println(index + " 我的線程標(biāo)識(shí)是:" + this.toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
方式一:newCachedThreadPool
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 描述:newCachedThreadPool
* 創(chuàng)建一個(gè)可緩存線程池吞彤,應(yīng)用中存在的線程數(shù)可以無限大
*/
public class ThreadpoolsCached {
public static void main(String[]args)
{
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
for(int i=0;i<10;i++)
{
final int index=i;
newCachedThreadPool.execute(new ThreadForpools(index));
}
}
}
借助jconsole工具看看線程我衬,由下圖可看。所有的線程一次性已經(jīng)加載進(jìn)來了饰恕。
線程執(zhí)行完了挠羔,線程數(shù)也相應(yīng)減少了,最后程序也就結(jié)束了埋嵌。
方式二:newFixedThreadPool
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 描述:創(chuàng)建一個(gè)定長線程池破加,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待雹嗦。
*/
public class ThreadpoolsFixed {
public static void main(String[]args)
{
//線程池允許同時(shí)存在兩個(gè)線程
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);
for(int i=0;i<10;i++)
{
final int index=i;
newFixedThreadPool.execute(new ThreadForpools(index));
}
}
}
再看看jconsole的線程情況范舀。只有兩個(gè)線程合是。線程執(zhí)行完后還會(huì)處于等待的狀態(tài)。
方式三:newScheduledThreadPool
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 描述:創(chuàng)建一個(gè)定長線程池锭环,支持定時(shí)及周期性任務(wù)執(zhí)行聪全。延遲執(zhí)行示例代碼如下
*/
public class ThreadpoolsScheduled {
/**
* 我們獲取四次次線程,觀察4個(gè)線程地址
* @param args
*/
public static void main(String[]args)
{
ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(2);
for(int i=0;i<10;i++)
{
final int index=i;
//延遲三秒執(zhí)行
newScheduledThreadPool.schedule(new ThreadForpools(index),3, TimeUnit.SECONDS);
}
}
}
方式四:newSingleThreadExecutor
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* 描述:創(chuàng)建一個(gè)單線程化的線程池辅辩,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù)难礼,
* 保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行。
*/
public class ThreadpoolsSingle {
public static void main(String[]args) {
ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
for(int i=0;i<10;i++)
{
final int index=i;
newSingleThreadExecutor.execute(new ThreadForpools(index));
}
}
}
方式五:newSingleThreadScheduledExecutor
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 描述:創(chuàng)建一個(gè)單例線程池玫锋,定期或延時(shí)執(zhí)行任務(wù)蛾茉。
*/
public class ThreadpoolsSingleThreadScheduled {
/**
* 我們獲取四次次線程,觀察4個(gè)線程地址
* @param args
*/
public static void main(String[]args) {
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
for(int i=0;i<10;i++)
{
final int index=i;
scheduledExecutorService.schedule(new ThreadForpools(index),3, TimeUnit.SECONDS);
}
}
}
方式六:
···
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
描述:創(chuàng)建一個(gè)帶并行級(jí)別的線程池撩鹿,并行級(jí)別決定了同一時(shí)刻最多有多少個(gè)線程在執(zhí)行谦炬,
-
如不穿如并行級(jí)別參數(shù),將默認(rèn)為當(dāng)前系統(tǒng)的CPU個(gè)數(shù)节沦。
*/
public class ThreadpoolsWorkStealingPool {
public static void main(String[] args) throws Exception {// 設(shè)置并行級(jí)別為2键思,即默認(rèn)每時(shí)每刻只有2個(gè)線程同時(shí)執(zhí)行 ExecutorService executorService = Executors.newWorkStealingPool(3); for (int i = 1; i <= 50; i++) { final int count=i; executorService.submit(new ThreadForpools(count)); } while(true){ //主線程陷入死循環(huán),來觀察結(jié)果散劫,否則是看不到結(jié)果的 }
}
}
···