- 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();
1.schedule
schedule(Runnable command, long delay, TimeUnit unit),schedule方法被用來延遲指定時間后執行某個指定任務。
a.代碼如下:
- public class Job implements Runnable {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- public void run() {
- try {
- Thread.sleep(5000);
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- System.out.println("do something at:" + sdf.format(new Date()));
- }
- }
- public class ScheduledExecutorServiceTest {
- public static void main(String[] args) {
- ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
- final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- System.out.println(" begin to do something at:" + sdf.format(new Date()));
- schedule.schedule(new Job(),1, TimeUnit.SECONDS);
- }
- }
b.輸出如下:
- begin to do something at:2012-08-03 09:31:36
- do something at:2012-08-03 09:31:42
注:此時程序不會推出,若想讓程序推出,需要加上schedule.shutdown();
ScheduledExecutorService 中兩種最常用的調度方法 ScheduleAtFixedRate 和 ScheduleWithFixedDelay。ScheduleAtFixedRate 每次執行時間為上一次任務開始起向后推一個時間間隔,即每次執行時間為 :initialDelay, initialDelay+period, initialDelay+2*period, …;ScheduleWithFixedDelay 每次執行時間為上一次任務結束起向后推一個時間間隔,即每次執行時間為:initialDelay, initialDelay+executeTime+delay, initialDelay+2*executeTime+2*delay。由此可見,ScheduleAtFixedRate 是基於固定時間間隔進行任務調度,ScheduleWithFixedDelay 取決於每次任務執行的時間長短,是基於不固定時間間隔進行任務調度。
2.scheduleWithFixedDelay
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit)
創建並執行一個在給定初始延遲后首次啟用的定期操作,隨后,在每一次執行終止和下一次執行開始之間都存在給定的延遲,如果任務的執行時間超過了廷遲時間(delay),下一個任務則會在
(當前任務執行所需時間+delay)后執行。
a.代碼如下:
- public class ScheduledExecutorServiceTest {
- public static void main(String[] args) {
- ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
- final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- System.out.println(" begin to do something at:" + sdf.format(new Date()));
- schedule.scheduleWithFixedDelay(new Job(), 1, 2, TimeUnit.SECONDS);
- }
- }
b.輸出如下:
- begin to do something at:2012-08-03 09:36:53
- do something at:2012-08-03 09:36:59
- do something at:2012-08-03 09:37:06
- do something at:2012-08-03 09:37:13
3.scheduleAtFixedRate
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnitunit)
創建並執行一個在給定初始延遲后首次啟用的定期操作,后續操作具有給定的周期;也就是將在 initialDelay 后開始執行,然后在initialDelay+period 后執行,接着在 initialDelay + 2 * period 后執行,依此類推。
如果任務的執行時間小於period,將會按上述規律執行。否則,則會按 任務的實際執行時間進行周期執行。
a.代碼如下:
- public class ScheduledExecutorServiceTest {
- public static void main(String[] args) {
- ScheduledExecutorService schedule = Executors.newScheduledThreadPool(2);
- final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- System.out.println(" begin to do something at:" + sdf.format(new Date()));
- schedule.scheduleAtFixedRate(new Job(), 1,2, TimeUnit.SECONDS);
- }
b.結果輸出:
- begin to do something at:2012-08-04 08:53:30
- do something at:2012-08-04 08:53:36
- do something at:2012-08-04 08:53:41
- do something at:2012-08-04 08:53:46
- do something at:2012-08-04 08:53:51

