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();

 

1.schedule
     schedule(Runnable command, long delay, TimeUnit unit),schedule方法被用來延遲指定時間后執行某個指定任務。
a.代碼如下:

Java代碼 收藏代碼

  1. public class Job implements Runnable { 
  2.     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
  3. public void run() { 
  4. try { 
  5.             Thread.sleep(5000); 
  6.         } catch (InterruptedException ex) { 
  7.             ex.printStackTrace(); 
  8.         } 
  9.         System.out.println("do something  at:" + sdf.format(new Date())); 
  10.     } 
  11. public class ScheduledExecutorServiceTest { 
  12. public static void main(String[] args) { 
  13.         ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5); 
  14. final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
  15.         System.out.println(" begin to do something at:" + sdf.format(new Date())); 
  16.         schedule.schedule(new Job(),1, TimeUnit.SECONDS); 
  17.     } 

b.輸出如下:

  1. begin to do something at:2012-08-03 09:31:36
  2. do something  at:2012-08-03 09:31:42

注:此時程序不會推出,若想讓程序推出,需要加上schedule.shutdown();

 

 

ScheduledExecutorService 中兩種最常用的調度方法 ScheduleAtFixedRateScheduleWithFixedDelay。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.代碼如下:

Java代碼 收藏代碼

  1. public class ScheduledExecutorServiceTest { 
  2. public static void main(String[] args) { 
  3.             ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5); 
  4. final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
  5.             System.out.println(" begin to do something at:" + sdf.format(new Date())); 
  6.             schedule.scheduleWithFixedDelay(new Job(), 1, 2, TimeUnit.SECONDS); 
  7.         } 
  8.     } 

    b.輸出如下:

  1. begin to do something at:2012-08-03 09:36:53
  2. do something at:2012-08-03 09:36:59
  3. do something at:2012-08-03 09:37:06
  4. 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.代碼如下:

  1. public class ScheduledExecutorServiceTest { 
  2. public static void main(String[] args) { 
  3.         ScheduledExecutorService schedule = Executors.newScheduledThreadPool(2); 
  4. final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
  5.         System.out.println(" begin to do something at:" + sdf.format(new Date())); 
  6.         schedule.scheduleAtFixedRate(new Job(), 1,2, TimeUnit.SECONDS); 
  7.     } 

    b.結果輸出:

  1. begin to do something at:2012-08-04 08:53:30
  2. do something at:2012-08-04 08:53:36
  3. do something at:2012-08-04 08:53:41
  4. do something at:2012-08-04 08:53:46
  5. do something at:2012-08-04 08:53:51


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM