java線程池常用方法收集

execute(Runnable command):履行Ruannable類型的任務

submit(task):可用來提交Callable或Runnable任務,並返回代表此任務的Future對象

invokeAll(collection of tasks):執行給定的任務,當所有任務完成時,返回保持任務狀態和結果的 Future 列表.

shutdown():在完成已提交的任務後封閉辦事,不再接管新任務

shutdownNow():停止所有正在履行的任務並封閉辦事。

isTerminated():測試是否所有任務都履行完畢了。

isShutdown():測試是否該ExecutorService已被封閉

 

1、固定大小線程池

import java.util.concurrent.Executors;  

import java.util.concurrent.ExecutorService;

ExecutorService pool = Executors.newFixedThreadPool(2);

pool.execute(t1);

pool.shutdown();

2、單任務線程池

ExecutorService pool = Executors.newSingleThreadExecutor();

3、可變尺寸線程池

ExecutorService pool = Executors.newCachedThreadPool();

4、延遲連接池

import java.util.concurrent.Executors;  

import java.util.concurrent.ScheduledExecutorService;  

import java.util.concurrent.TimeUnit;

ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);

pool.schedule(t4, 10, TimeUnit.MILLISECONDS);

5、單任務延遲連接池

ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章