線程池,java線程池

來源:互聯網
上載者:User

線程池,java線程池

  為了更好的控制多線程,JDK提供了一套線程架構Executor,協助開發人員有效地進行線程式控制制。他們都在java.util.concurrent包中,是JDK並發包的核心。
其中有一個比較重要的類:Executors,它扮演著線程工廠的角色,我們通過Executors可以建立特定功能的線程池。
Executors建立線程池的方法:
newFixedThreadPool()方法,該方法返回一個固定數量的線程池,該方法的線程數量始終不變,當有一個任務提交時,若線程池中空閑,則立即執行,
若沒有,則會被暫緩在一個任務隊列中等待有閒置線程去執行。
newSingleThreadExecutor()方法,建立一個線程的線程池,若空閑則執行,若沒有空閑線程則暫緩在任務隊列中。
newCachedThreadPool()方法,返回一個可根據實際情況調整線程個數的線程池,不限制最大線程數量,若有閒置線程,則執行任務;若無任務則不建立線程,
並且每一個空閑線程會在60秒後自動回收。
newScheduledThreadPool()方法,該方法返回一個SchededExecutorService對象,但線程池可以指定線程的數量。
主函數:
 1 package executor; 2  3 import java.util.concurrent.ArrayBlockingQueue; 4 import java.util.concurrent.ThreadPoolExecutor; 5 import java.util.concurrent.TimeUnit; 6  7 /** 8  * Created by Administrator on 2017/5/17. 9  */10 public class UseThreadPoolExecutor1 {11 12     public static void main(String[] args) {13         /**14          * 在使用有界隊列時,若有新的任務需要執行,如果線程池實際線程數小於corePoolSize,則優先建立線程,15          * 若大於corePoolSize,則會將任務排入佇列,16          * 若隊列已滿,則在匯流排程數不大於maximumPoolSize的前提下,建立新的線程,17          * 若線程數大於maximumPoolSize,則執行拒絕策略。或其他自訂方式。18          */19         ThreadPoolExecutor pool = new ThreadPoolExecutor(20                 1,                 //coreSize21                 2,                 //MaxSize22                 60,             //6023                 TimeUnit.SECONDS,24                 new ArrayBlockingQueue<Runnable>(3)            //指定一種隊列 (有界隊列)25                 //new LinkedBlockingQueue<Runnable>()26                 , new MyRejected()27                 //, new DiscardOldestPolicy()28         );29 30         MyTask mt1 = new MyTask(1, "任務1");31         MyTask mt2 = new MyTask(2, "任務2");32         MyTask mt3 = new MyTask(3, "任務3");33         MyTask mt4 = new MyTask(4, "任務4");34         MyTask mt5 = new MyTask(5, "任務5");35         MyTask mt6 = new MyTask(6, "任務6");36 37         pool.execute(mt1);38         pool.execute(mt2);39         pool.execute(mt3);40         pool.execute(mt4);41         pool.execute(mt5);42         pool.execute(mt6);43 44         pool.shutdown();45 46     }47 }
MyTask類
 1 package executor; 2  3 /** 4  * Created by Administrator on 2017/5/17. 5  */ 6 public class MyTask implements Runnable { 7     private int taskId; 8     private String taskName; 9 10     public MyTask(int taskId, String taskName){11         this.taskId = taskId;12         this.taskName = taskName;13     }14 15     public int getTaskId() {16         return taskId;17     }18 19     public void setTaskId(int taskId) {20         this.taskId = taskId;21     }22 23     public String getTaskName() {24         return taskName;25     }26 27     public void setTaskName(String taskName) {28         this.taskName = taskName;29     }30 31     @Override32     public void run() {33         try {34             System.out.println("run taskId =" + this.taskId);35             Thread.sleep(5*1000);36             //System.out.println("end taskId =" + this.taskId);37         } catch (InterruptedException e) {38             e.printStackTrace();39         }40     }41 42     public String toString(){43         return Integer.toString(this.taskId);44     }45 }
MyRejected類:
 1 package executor; 2  3 import java.util.concurrent.RejectedExecutionHandler; 4 import java.util.concurrent.ThreadPoolExecutor; 5  6 /** 7  * Created by Administrator on 2017/5/17. 8  */ 9 public class MyRejected implements RejectedExecutionHandler {10 11     public MyRejected(){12     }13 14     @Override15     public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {16         System.out.println("自訂處理..");17         System.out.println("當前被拒絕任務為:" + r.toString());18 19 20     }21 }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.