前言
Java為什么引入線程池?
創(chuàng)建線程示例
new Thread(new Runnable() {
@Override
public void run() {
}
}).start();
new Thread的弊端
- 每次new Thread新建對象性能差。
- 線程缺乏統(tǒng)一管理,可能無限制新建線程,相互之間競爭,及可能占用過多系統(tǒng)資源導(dǎo)致死機(jī)或oom。
- 缺乏更多功能,如定時執(zhí)行、定期執(zhí)行、線程中斷。
相比new Thread,Java提供的四種線程池的好處在于:
- 重用存在的線程,減少對象創(chuàng)建、消亡的開銷,性能佳。
- 可有效控制最大并發(fā)線程數(shù),提高系統(tǒng)資源的使用率,同時避免過多資源競爭,避免堵塞。
-
提供定時執(zhí)行、定期執(zhí)行、單線程、并發(fā)數(shù)控制等功能。
image.png
1.Java四種線程池
Java通過Executors提供四種線程池,分別為:
newFixedThreadPool 創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會在隊(duì)列中等待。
newSingleThreadExecutor 創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。
newCachedThreadPool創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
newScheduledThreadPool 創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行。
1.1 newFixedThreadPool
創(chuàng)建一個定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會在隊(duì)列中等待。
public class ThreadPoolTest {
public static void main(String[] args) {
//固定線程池
ExecutorService threadPool = Executors.newFixedThreadPool(3);
for (int i = 1; i < 10; i++) {
int finalI = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
for (int j = 1; j < 10; j++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI
);
}
}
});
}
}
}
pool-1-thread-3 is looping of 1 for task of 3
pool-1-thread-2 is looping of 1 for task of 2
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-1 is looping of 1 for task of 4
pool-1-thread-2 is looping of 1 for task of 5
pool-1-thread-3 is looping of 1 for task of 6
pool-1-thread-1 is looping of 2 for task of 4
pool-1-thread-2 is looping of 2 for task of 5
pool-1-thread-3 is looping of 2 for task of 6
pool-1-thread-1 is looping of 3 for task of 4
pool-1-thread-2 is looping of 3 for task of 5
pool-1-thread-3 is looping of 3 for task of 6
pool-1-thread-3 is looping of 4 for task of 6
pool-1-thread-1 is looping of 4 for task of 4
pool-1-thread-2 is looping of 4 for task of 5
pool-1-thread-3 is looping of 5 for task of 6
pool-1-thread-1 is looping of 5 for task of 4
pool-1-thread-2 is looping of 5 for task of 5
pool-1-thread-2 is looping of 6 for task of 5
pool-1-thread-1 is looping of 6 for task of 4
pool-1-thread-3 is looping of 6 for task of 6
pool-1-thread-2 is looping of 7 for task of 5
pool-1-thread-3 is looping of 7 for task of 6
pool-1-thread-1 is looping of 7 for task of 4
pool-1-thread-1 is looping of 8 for task of 4
pool-1-thread-3 is looping of 8 for task of 6
pool-1-thread-2 is looping of 8 for task of 5
pool-1-thread-3 is looping of 9 for task of 6
pool-1-thread-2 is looping of 9 for task of 5
pool-1-thread-1 is looping of 9 for task of 4
pool-1-thread-2 is looping of 1 for task of 8
pool-1-thread-1 is looping of 1 for task of 9
pool-1-thread-3 is looping of 1 for task of 7
pool-1-thread-1 is looping of 2 for task of 9
pool-1-thread-2 is looping of 2 for task of 8
pool-1-thread-3 is looping of 2 for task of 7
pool-1-thread-1 is looping of 3 for task of 9
pool-1-thread-2 is looping of 3 for task of 8
pool-1-thread-3 is looping of 3 for task of 7
pool-1-thread-1 is looping of 4 for task of 9
pool-1-thread-2 is looping of 4 for task of 8
pool-1-thread-3 is looping of 4 for task of 7
pool-1-thread-2 is looping of 5 for task of 8
pool-1-thread-3 is looping of 5 for task of 7
pool-1-thread-1 is looping of 5 for task of 9
pool-1-thread-2 is looping of 6 for task of 8
pool-1-thread-1 is looping of 6 for task of 9
pool-1-thread-3 is looping of 6 for task of 7
pool-1-thread-3 is looping of 7 for task of 7
pool-1-thread-1 is looping of 7 for task of 9
pool-1-thread-2 is looping of 7 for task of 8
pool-1-thread-3 is looping of 8 for task of 7
pool-1-thread-1 is looping of 8 for task of 9
pool-1-thread-2 is looping of 8 for task of 8
pool-1-thread-3 is looping of 9 for task of 7
pool-1-thread-2 is looping of 9 for task of 8
pool-1-thread-1 is looping of 9 for task of 9
定長線程池的大小最好根據(jù)系統(tǒng)資源進(jìn)行設(shè)置。如Runtime.getRuntime().availableProcessors()
1.2 newFixedThreadPool
創(chuàng)建一個單線程化的線程池,它只會用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。
public class ThreadPoolTest {
public static void main(String[] args) {
//固定線程池
ExecutorService threadPool = Executors.newSingleThreadExecutor();
for (int i = 1; i < 10; i++) {
int finalI = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
for (int j = 1; j < 10; j++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI
);
}
}
});
}
}
}
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-1 is looping of 1 for task of 2
pool-1-thread-1 is looping of 2 for task of 2
pool-1-thread-1 is looping of 3 for task of 2
pool-1-thread-1 is looping of 4 for task of 2
pool-1-thread-1 is looping of 5 for task of 2
pool-1-thread-1 is looping of 6 for task of 2
pool-1-thread-1 is looping of 7 for task of 2
pool-1-thread-1 is looping of 8 for task of 2
pool-1-thread-1 is looping of 9 for task of 2
pool-1-thread-1 is looping of 1 for task of 3
pool-1-thread-1 is looping of 2 for task of 3
pool-1-thread-1 is looping of 3 for task of 3
pool-1-thread-1 is looping of 4 for task of 3
pool-1-thread-1 is looping of 5 for task of 3
pool-1-thread-1 is looping of 6 for task of 3
pool-1-thread-1 is looping of 7 for task of 3
pool-1-thread-1 is looping of 8 for task of 3
pool-1-thread-1 is looping of 9 for task of 3
pool-1-thread-1 is looping of 1 for task of 4
pool-1-thread-1 is looping of 2 for task of 4
pool-1-thread-1 is looping of 3 for task of 4
pool-1-thread-1 is looping of 4 for task of 4
pool-1-thread-1 is looping of 5 for task of 4
pool-1-thread-1 is looping of 6 for task of 4
pool-1-thread-1 is looping of 7 for task of 4
pool-1-thread-1 is looping of 8 for task of 4
pool-1-thread-1 is looping of 9 for task of 4
pool-1-thread-1 is looping of 1 for task of 5
pool-1-thread-1 is looping of 2 for task of 5
pool-1-thread-1 is looping of 3 for task of 5
pool-1-thread-1 is looping of 4 for task of 5
pool-1-thread-1 is looping of 5 for task of 5
pool-1-thread-1 is looping of 6 for task of 5
pool-1-thread-1 is looping of 7 for task of 5
pool-1-thread-1 is looping of 8 for task of 5
pool-1-thread-1 is looping of 9 for task of 5
pool-1-thread-1 is looping of 1 for task of 6
pool-1-thread-1 is looping of 2 for task of 6
pool-1-thread-1 is looping of 3 for task of 6
pool-1-thread-1 is looping of 4 for task of 6
pool-1-thread-1 is looping of 5 for task of 6
pool-1-thread-1 is looping of 6 for task of 6
pool-1-thread-1 is looping of 7 for task of 6
pool-1-thread-1 is looping of 8 for task of 6
pool-1-thread-1 is looping of 9 for task of 6
pool-1-thread-1 is looping of 1 for task of 7
pool-1-thread-1 is looping of 2 for task of 7
pool-1-thread-1 is looping of 3 for task of 7
pool-1-thread-1 is looping of 4 for task of 7
pool-1-thread-1 is looping of 5 for task of 7
pool-1-thread-1 is looping of 6 for task of 7
pool-1-thread-1 is looping of 7 for task of 7
pool-1-thread-1 is looping of 8 for task of 7
pool-1-thread-1 is looping of 9 for task of 7
pool-1-thread-1 is looping of 1 for task of 8
pool-1-thread-1 is looping of 2 for task of 8
pool-1-thread-1 is looping of 3 for task of 8
pool-1-thread-1 is looping of 4 for task of 8
pool-1-thread-1 is looping of 5 for task of 8
pool-1-thread-1 is looping of 6 for task of 8
pool-1-thread-1 is looping of 7 for task of 8
pool-1-thread-1 is looping of 8 for task of 8
pool-1-thread-1 is looping of 9 for task of 8
pool-1-thread-1 is looping of 1 for task of 9
pool-1-thread-1 is looping of 2 for task of 9
pool-1-thread-1 is looping of 3 for task of 9
pool-1-thread-1 is looping of 4 for task of 9
pool-1-thread-1 is looping of 5 for task of 9
pool-1-thread-1 is looping of 6 for task of 9
pool-1-thread-1 is looping of 7 for task of 9
pool-1-thread-1 is looping of 8 for task of 9
pool-1-thread-1 is looping of 9 for task of 9
結(jié)果依次輸出,相當(dāng)于順序執(zhí)行各個任務(wù)。
現(xiàn)行大多數(shù)GUI程序都是單線程的。Android中單線程可用于數(shù)據(jù)庫操作,文件操作,應(yīng)用批量安裝,應(yīng)用批量刪除等不適合并發(fā)但可能IO阻塞性及影響UI線程響應(yīng)的操作。
1.3 newCachedThreadPool
創(chuàng)建一個可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程
public class ThreadPoolTest {
public static void main(String[] args) {
//固定線程池
ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 1; i < 10; i++) {
int finalI = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
for (int j = 1; j < 10; j++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI
);
}
}
});
}
}
}
pool-1-thread-2 is looping of 1 for task of 2
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-8 is looping of 1 for task of 8
pool-1-thread-9 is looping of 1 for task of 9
pool-1-thread-6 is looping of 1 for task of 6
pool-1-thread-3 is looping of 1 for task of 3
pool-1-thread-7 is looping of 1 for task of 7
pool-1-thread-4 is looping of 1 for task of 4
pool-1-thread-5 is looping of 1 for task of 5
pool-1-thread-6 is looping of 2 for task of 6
pool-1-thread-5 is looping of 2 for task of 5
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-7 is looping of 2 for task of 7
pool-1-thread-8 is looping of 2 for task of 8
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-9 is looping of 2 for task of 9
pool-1-thread-4 is looping of 2 for task of 4
pool-1-thread-5 is looping of 3 for task of 5
pool-1-thread-9 is looping of 3 for task of 9
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-8 is looping of 3 for task of 8
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-7 is looping of 3 for task of 7
pool-1-thread-6 is looping of 3 for task of 6
pool-1-thread-4 is looping of 3 for task of 4
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-6 is looping of 4 for task of 6
pool-1-thread-4 is looping of 4 for task of 4
pool-1-thread-7 is looping of 4 for task of 7
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-9 is looping of 4 for task of 9
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-5 is looping of 4 for task of 5
pool-1-thread-8 is looping of 4 for task of 8
pool-1-thread-6 is looping of 5 for task of 6
pool-1-thread-9 is looping of 5 for task of 9
pool-1-thread-5 is looping of 5 for task of 5
pool-1-thread-8 is looping of 5 for task of 8
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-7 is looping of 5 for task of 7
pool-1-thread-4 is looping of 5 for task of 4
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-6 is looping of 6 for task of 6
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-4 is looping of 6 for task of 4
pool-1-thread-7 is looping of 6 for task of 7
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-5 is looping of 6 for task of 5
pool-1-thread-8 is looping of 6 for task of 8
pool-1-thread-9 is looping of 6 for task of 9
pool-1-thread-4 is looping of 7 for task of 4
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-5 is looping of 7 for task of 5
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-9 is looping of 7 for task of 9
pool-1-thread-8 is looping of 7 for task of 8
pool-1-thread-7 is looping of 7 for task of 7
pool-1-thread-6 is looping of 7 for task of 6
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-9 is looping of 8 for task of 9
pool-1-thread-5 is looping of 8 for task of 5
pool-1-thread-7 is looping of 8 for task of 7
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-4 is looping of 8 for task of 4
pool-1-thread-8 is looping of 8 for task of 8
pool-1-thread-6 is looping of 8 for task of 6
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-5 is looping of 9 for task of 5
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-4 is looping of 9 for task of 4
pool-1-thread-6 is looping of 9 for task of 6
pool-1-thread-8 is looping of 9 for task of 8
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-7 is looping of 9 for task of 7
pool-1-thread-9 is looping of 9 for task of 9
線程池為無限大,當(dāng)執(zhí)行第二個任務(wù)時第一個任務(wù)已經(jīng)完成,會復(fù)用執(zhí)行第一個任務(wù)的線程,而不用每次新建線程。
1.4 newScheduledThreadPool
public class ThreadPoolTest {
public static void main(String[] args) {
System.out.println("init");
Executors.newScheduledThreadPool(3).schedule(
new Runnable() {
@Override
public void run() {
System.out.println("Done");
}
}, 3, TimeUnit.SECONDS
);
}
}
init
Done
表示延遲3秒執(zhí)行。
定期執(zhí)行
public class ThreadPoolTest {
public static void main(String[] args) {
Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("每三秒執(zhí)行一次");
}
},2,3,TimeUnit.SECONDS);
}
}
每三秒執(zhí)行一次
每三秒執(zhí)行一次
每三秒執(zhí)行一次
每三秒執(zhí)行一次
省略...
2 關(guān)閉線程池
shutdown()
public class ThreadPoolTest {
public static void main(String[] args) {
//固定線程池
ExecutorService threadPool = Executors.newFixedThreadPool(3);
//緩存線程池 池中的線程數(shù)量
//ExecutorService threadPool = Executors.newCachedThreadPool();
//單個線程
//ExecutorService threadPool = Executors.newSingleThreadExecutor();
for (int i = 1; i < 10; i++) {
int finalI = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
for (int j = 1; j < 10; j++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI
);
}
}
});
}
threadPool.shutdown();
}
}
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-2 is looping of 1 for task of 2
pool-1-thread-3 is looping of 1 for task of 3
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-1 is looping of 9 for task of 1
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-1 is looping of 1 for task of 4
pool-1-thread-3 is looping of 1 for task of 5
pool-1-thread-2 is looping of 1 for task of 6
pool-1-thread-1 is looping of 2 for task of 4
pool-1-thread-3 is looping of 2 for task of 5
pool-1-thread-2 is looping of 2 for task of 6
pool-1-thread-1 is looping of 3 for task of 4
pool-1-thread-3 is looping of 3 for task of 5
pool-1-thread-2 is looping of 3 for task of 6
pool-1-thread-1 is looping of 4 for task of 4
pool-1-thread-3 is looping of 4 for task of 5
pool-1-thread-2 is looping of 4 for task of 6
pool-1-thread-3 is looping of 5 for task of 5
pool-1-thread-1 is looping of 5 for task of 4
pool-1-thread-2 is looping of 5 for task of 6
pool-1-thread-3 is looping of 6 for task of 5
pool-1-thread-1 is looping of 6 for task of 4
pool-1-thread-2 is looping of 6 for task of 6
pool-1-thread-3 is looping of 7 for task of 5
pool-1-thread-1 is looping of 7 for task of 4
pool-1-thread-2 is looping of 7 for task of 6
pool-1-thread-3 is looping of 8 for task of 5
pool-1-thread-1 is looping of 8 for task of 4
pool-1-thread-2 is looping of 8 for task of 6
pool-1-thread-3 is looping of 9 for task of 5
pool-1-thread-1 is looping of 9 for task of 4
pool-1-thread-2 is looping of 9 for task of 6
pool-1-thread-1 is looping of 1 for task of 8
pool-1-thread-2 is looping of 1 for task of 9
pool-1-thread-3 is looping of 1 for task of 7
pool-1-thread-1 is looping of 2 for task of 8
pool-1-thread-2 is looping of 2 for task of 9
pool-1-thread-3 is looping of 2 for task of 7
pool-1-thread-1 is looping of 3 for task of 8
pool-1-thread-2 is looping of 3 for task of 9
pool-1-thread-3 is looping of 3 for task of 7
pool-1-thread-1 is looping of 4 for task of 8
pool-1-thread-2 is looping of 4 for task of 9
pool-1-thread-3 is looping of 4 for task of 7
pool-1-thread-2 is looping of 5 for task of 9
pool-1-thread-3 is looping of 5 for task of 7
pool-1-thread-1 is looping of 5 for task of 8
pool-1-thread-2 is looping of 6 for task of 9
pool-1-thread-3 is looping of 6 for task of 7
pool-1-thread-1 is looping of 6 for task of 8
pool-1-thread-2 is looping of 7 for task of 9
pool-1-thread-3 is looping of 7 for task of 7
pool-1-thread-1 is looping of 7 for task of 8
pool-1-thread-3 is looping of 8 for task of 7
pool-1-thread-1 is looping of 8 for task of 8
pool-1-thread-2 is looping of 8 for task of 9
pool-1-thread-3 is looping of 9 for task of 7
pool-1-thread-1 is looping of 9 for task of 8
pool-1-thread-2 is looping of 9 for task of 9
shutdownNow
public class ThreadPoolTest {
public static void main(String[] args) {
//固定線程池
ExecutorService threadPool = Executors.newFixedThreadPool(3);
//緩存線程池 池中的線程數(shù)量
//ExecutorService threadPool = Executors.newCachedThreadPool();
//單個線程
//ExecutorService threadPool = Executors.newSingleThreadExecutor();
for (int i = 1; i < 10; i++) {
int finalI = i;
threadPool.execute(new Runnable() {
@Override
public void run() {
for (int j = 1; j < 10; j++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + finalI
);
}
}
});
}
threadPool.shutdownNow();
}
}
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at ThreadPoolTest$1.run(ThreadPoolTest.java:23)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.lang.InterruptedException: sleep interrupted
pool-1-thread-3 is looping of 1 for task of 3
at java.lang.Thread.sleep(Native Method)
at ThreadPoolTest$1.run(ThreadPoolTest.java:23)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
pool-1-thread-2 is looping of 1 for task of 2
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at ThreadPoolTest$1.run(ThreadPoolTest.java:23)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
pool-1-thread-1 is looping of 1 for task of 1
pool-1-thread-3 is looping of 2 for task of 3
pool-1-thread-1 is looping of 2 for task of 1
pool-1-thread-2 is looping of 2 for task of 2
pool-1-thread-2 is looping of 3 for task of 2
pool-1-thread-1 is looping of 3 for task of 1
pool-1-thread-3 is looping of 3 for task of 3
pool-1-thread-2 is looping of 4 for task of 2
pool-1-thread-3 is looping of 4 for task of 3
pool-1-thread-1 is looping of 4 for task of 1
pool-1-thread-2 is looping of 5 for task of 2
pool-1-thread-1 is looping of 5 for task of 1
pool-1-thread-3 is looping of 5 for task of 3
pool-1-thread-1 is looping of 6 for task of 1
pool-1-thread-2 is looping of 6 for task of 2
pool-1-thread-3 is looping of 6 for task of 3
pool-1-thread-1 is looping of 7 for task of 1
pool-1-thread-3 is looping of 7 for task of 3
pool-1-thread-2 is looping of 7 for task of 2
pool-1-thread-1 is looping of 8 for task of 1
pool-1-thread-3 is looping of 8 for task of 3
pool-1-thread-2 is looping of 8 for task of 2
pool-1-thread-3 is looping of 9 for task of 3
pool-1-thread-2 is looping of 9 for task of 2
pool-1-thread-1 is looping of 9 for task of 1
2.1 shutdown和shutdownNow的區(qū)別
shutdown只是將線程池的狀態(tài)設(shè)置為SHUTWDOWN狀態(tài),正在執(zhí)行的任務(wù)會繼續(xù)執(zhí)行下去,沒有被執(zhí)行的則中斷。而shutdownNow則是將線程池的狀態(tài)設(shè)置為STOP,正在執(zhí)行的任務(wù)則被停止,沒被執(zhí)行任務(wù)的則返回。
2.3 源碼分析shutdown和shutdownNow
1.shutdown源碼
public void shutdown() {
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(SHUTDOWN);
interruptIdleWorkers();
onShutdown(); // hook for ScheduledThreadPoolExecutor
} finally {
mainLock.unlock();
}
tryTerminate();
}
2.shutdownNow源碼
public List<Runnable> shutdownNow() {
List<Runnable> tasks;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
checkShutdownAccess();
advanceRunState(STOP);
interruptWorkers();
tasks = drainQueue();
} finally {
mainLock.unlock();
}
tryTerminate();
return tasks;
}
從源碼可以很清晰的看出兩者的區(qū)別,shutdown使用了以后會置狀態(tài)為SHUTDOWN,而shutdownNow為STOP。此外,shutdownNow會返回任務(wù)列表。
2.3 線程狀態(tài)知識延伸
在ThreadPoolExecutor中定義了關(guān)于線程狀態(tài)的幾個變量如下:
// runState is stored in the high-order bits
private static final int RUNNING = -1 << COUNT_BITS;
private static final int SHUTDOWN = 0 << COUNT_BITS;
private static final int STOP = 1 << COUNT_BITS;
private static final int TIDYING = 2 << COUNT_BITS;
private static final int TERMINATED = 3 << COUNT_BITS;
1.當(dāng)創(chuàng)建線程池后,初始時,線程池處于RUNNING狀態(tài),此時線程池中的任務(wù)為0;
2.如果調(diào)用了shutdown()方法,則線程池處于SHUTDOWN狀態(tài),此時線程池不能夠接受新的任務(wù),它會等待所有任務(wù)執(zhí)行完畢;
3.如果調(diào)用了shutdownNow()方法,則線程池處于STOP狀態(tài),此時線程池不能接受新的任務(wù),并且會去嘗試終止正在執(zhí)行的任務(wù);
4.當(dāng)所有的任務(wù)已終止,ctl記錄的”任務(wù)數(shù)量”為0,線程池會變?yōu)門IDYING狀態(tài)。接著會執(zhí)行terminated()函數(shù)。
5.線程池處在TIDYING狀態(tài)時,執(zhí)行完terminated()之后,就會由 TIDYING -> TERMINATED,線程池被設(shè)置為TERMINATED狀態(tài)。
總結(jié)
線程池的作用:
線程池作用就是限制系統(tǒng)中執(zhí)行線程的數(shù)量。
根據(jù)系統(tǒng)的環(huán)境情況,可以自動或手動設(shè)置線程數(shù)量,達(dá)到運(yùn)行的最佳效果;少了浪費(fèi)了系統(tǒng)資源,多了造成系統(tǒng)擁擠效率不高。用線程池控制線程數(shù)量,其他線程排隊(duì)等候。一個任務(wù)執(zhí)行完畢,再從隊(duì)列的中取最前面的任務(wù)開始執(zhí)行。若隊(duì)列中沒有等待進(jìn)程,線程池的這一資源處于等待。當(dāng)一個新任務(wù)需要運(yùn)行時,如果線程池 中有等待的工作線程,就可以開始運(yùn)行了;否則進(jìn)入等待隊(duì)列。
為什么要用線程池:
1.減少了創(chuàng)建和銷毀線程的次數(shù),每個工作線程都可以被重復(fù)利用,可執(zhí)行多個任務(wù)。
2.可以根據(jù)系統(tǒng)的承受能力,調(diào)整線程池中工作線線程的數(shù)目,防止因?yàn)橄倪^多的內(nèi)存,而把服務(wù)器累趴下(每個線程需要大約1MB內(nèi)存,線程開的越多,消耗的內(nèi)存也就越大,最后死機(jī))。
