Java-五種線程池,四種拒絕策略,三種阻塞隊列:
三種阻塞隊列:
BlockingQueue<Runnable> workQueue = null;
workQueue = new ArrayBlockingQueue<>(5);//基於數組的先進先出隊列,有界
workQueue = new LinkedBlockingQueue<>();//基於鏈表的先進先出隊列,無界
workQueue = new SynchronousQueue<>();//沒有容量,是無緩衝等待隊列,是一個不存儲元素的阻塞隊列,會直接將任務交給消費者,必須等隊列中的添加元素被消費後才能繼續添加新的元素。
四種拒絕策略:
RejectedExecutionHandler rejected = null;
rejected = new ThreadPoolExecutor.AbortPolicy();//默認,隊列滿了丟任務拋出異常
rejected = new ThreadPoolExecutor.DiscardPolicy();//隊列滿了丟任務不異常
rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//將最早進入隊列的任務刪,之後再嘗試加入隊列
rejected = new ThreadPoolExecutor.CallerRunsPolicy();//如果添加到線程池失敗,那麼主線程會自己去執行該任務
五種線程池:
ExecutorService threadPool = null;
threadPool = Executors.newCachedThreadPool();//有緩衝的線程池,線程數 JVM 控制
threadPool = Executors.newFixedThreadPool(3);//固定大小的線程池
threadPool = Executors.newScheduledThreadPool(2);
threadPool = Executors.newSingleThreadExecutor();//單線程的線程池,只有一個線程在工作
threadPool = new ThreadPoolExecutor();//默認線程池,可控制參數比較多
public static void main (String[] args) throws Exception {
testThreadPoolExecutor();
}
public static void testThreadPoolExecutor() throws Exception {
//基礎參數
int corePoolSize=2;//最小活躍線程數
int maximumPoolSize=5;//最大活躍線程數
int keepAliveTime=5;//指定線程池中線程空閒超過 5s 後將被回收
TimeUnit unit = TimeUnit.SECONDS;//keepAliveTime 單位
//阻塞隊列
BlockingQueue<Runnable> workQueue = null;
workQueue = new ArrayBlockingQueue<>(5);//基於數組的先進先出隊列,有界
workQueue = new LinkedBlockingQueue<>();//基於鏈表的先進先出隊列,無界
workQueue = new SynchronousQueue<>();//無緩衝的等待隊列,無界
//拒絕策略
RejectedExecutionHandler rejected = null;
rejected = new ThreadPoolExecutor.AbortPolicy();//默認,隊列滿了丟任務拋出異常
rejected = new ThreadPoolExecutor.DiscardPolicy();//隊列滿了丟任務不異常
rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//將最早進入隊列的任務刪,之後再嘗試加入隊列
rejected = new ThreadPoolExecutor.CallerRunsPolicy();//如果添加到線程池失敗,那麼主線程會自己去執行該任務
//使用的線程池
ExecutorService threadPool = null;
threadPool = Executors.newCachedThreadPool();//有緩衝的線程池,線程數 JVM 控制
threadPool = Executors.newFixedThreadPool(3);//固定大小的線程池
threadPool = Executors.newScheduledThreadPool(2);
threadPool = Executors.newSingleThreadExecutor();//單線程的線程池,只有一個線程在工作
threadPool = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
workQueue,
rejected);//默認線程池,可控制參數比較多
//執行無返回值線程
TaskRunnable taskRunnable = new TaskRunnable();
threadPool.execute(taskRunnable);
List<Future<String>> futres = new ArrayList<>();
for(int i=0;i<10;i++) {
//執行有返回值線程
TaskCallable taskCallable = new TaskCallable(i);
Future<String> future = threadPool.submit(taskCallable);
futres.add(future);
}
for(int i=0;i<futres.size();i++){
String result = futres.get(i).get();
System.out.println(i+" result = "+result);
}
}
/**
* 返回值的線程,使用 threadpool.execut() 執行
*/
public static class TaskRunnable implements Runnable{
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " runnable result!");
}
}
/**
* 有返回值的線程,使用 threadpool.submit() 執行
*/
public static class TaskCallable implements Callable<String>{
public TaskCallable(int index){
this.i=index;
}
private int i;
@Override
public String call() throws Exception {
int r = new Random().nextInt(5);
try {
Thread.sleep(r);
} catch (InterruptedException e) {
e.printStackTrace();
}
//System.out.println("callable result!");
return Thread.currentThread().getName()+" callable index="+i +",sleep="+r;
}
}