常用線程池:
- newSingleThreadExecutor創(chuàng)建一個單線程的線程池。這個線程池只有一個線程在工作,也就是相當于單線程串行執(zhí)行所有任務。如果這個唯一的線程因為異常結束,那么會有一個新的線程來替代它。此線程池保證所有任務的執(zhí)行順序按照任務的提交順序執(zhí)行。
- newFixedThreadPool創(chuàng)建固定大小的線程池。每次提交一個任務就創(chuàng)建一個線程,直到線程達到線程池的最大大小。線程池的大小一旦達到最大值就會保持不變,如果某個線程因為執(zhí)行異常而結束,那么線程池會補充一個新線程。
- newCachedThreadPool創(chuàng)建一個可緩存的線程池。如果線程池的大小超過了處理任務所需要的線程,那么就會回收部分空閑(60秒不執(zhí)行任務)的線程,當任務數增加時,此線程池又可以智能的添加新線程來處理任務。此線程池不會對線程池大小做限制,線程池大小完全依賴于操作系統(tǒng)(或者說JVM)能夠創(chuàng)建的最大線程大小。
- newScheduledThreadPool創(chuàng)建一個大小無限的線程池。此線程池支持定時以及周期性執(zhí)行任務的需求。
案例:
MyThread.java
public class MyThread extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "正在執(zhí)行。。。");
}
}
1. newSingleThreadExecutor
TestSingleThreadExecutor.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestSingleThreadExecutor {
public static void main(String[] args) {
// 創(chuàng)建一個可重用固定線程數的線程池
ExecutorService pool = Executors.newSingleThreadExecutor();
// 創(chuàng)建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
// 將線程放入池中進行執(zhí)行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
// 關閉線程池
pool.shutdown();
}
}
輸出:
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-1正在執(zhí)行。。。
2. newFixedThreadPool
TestFixedThreadPool.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestFixedThreadPool {
public static void main(String[] args) {
// 創(chuàng)建一個可重用固定線程數的線程池
ExecutorService pool = Executors.newFixedThreadPool(2);
// 創(chuàng)建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
// 將線程放入池中進行執(zhí)行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
// 關閉線程池
pool.shutdown();
}
}
輸出:
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-2正在執(zhí)行。。。
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-2正在執(zhí)行。。。
3. newCachedThreadPool
TestCachedThreadPool.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestCachedThreadPool {
public static void main(String[] args) {
// 創(chuàng)建一個可緩存的線程池
ExecutorService pool = Executors.newCachedThreadPool();
// 創(chuàng)建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
// 將線程放入池中進行執(zhí)行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
// 關閉線程池
pool.shutdown();
}
}
輸出:
pool-1-thread-2正在執(zhí)行。。。
pool-1-thread-3正在執(zhí)行。。。
pool-1-thread-1正在執(zhí)行。。。
pool-1-thread-4正在執(zhí)行。。。
pool-1-thread-3正在執(zhí)行。。。
4. newScheduledThreadPool
TestScheduledThreadPoolExecutor.java
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestScheduledThreadPoolExecutor {
public static void main(String[] args) {
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleAtFixedRate(new Runnable() {// 每隔一段時間就觸發(fā)異常
@Override
public void run() {
// throw
new RuntimeException();
System.out.println("****************");
}
}, 1000, 5000, TimeUnit.MILLISECONDS);
exec.scheduleAtFixedRate(new Runnable() {// 每隔一段時間打印系統(tǒng)時間,證明兩者是互不影響的
@Override
public void run() {
System.out.println(System.nanoTime());
}
}, 1000, 2000, TimeUnit.MILLISECONDS);
}
}
輸出:
70048556901803
70050554893733
70052557432636
70054555593719
70056554690062