线程池 Executors2
生活随笔
收集整理的這篇文章主要介紹了
线程池 Executors2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class UseThreadPoolExecutor1 {public static void main(String[] args) {/*** 在使用有界隊列時,若有新的任務需要執行,如果線程池實際線程數小于corePoolSize,則優先創建線程,* 若大于corePoolSize,則會將任務加入隊列,* 若隊列已滿,則在總線程數不大于maximumPoolSize的前提下,創建新的線程,* 若線程數大于maximumPoolSize,則執行拒絕策略。或其他自定義方式。* */ ThreadPoolExecutor pool = new ThreadPoolExecutor(1, //coreSize2, //MaxSize60, //60TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3) //指定一種隊列 (有界隊列)//new LinkedBlockingQueue<Runnable>(), new MyRejected()
// , new DiscardOldestPolicy());MyTask mt1 = new MyTask(1, "任務1");MyTask mt2 = new MyTask(2, "任務2");MyTask mt3 = new MyTask(3, "任務3");MyTask mt4 = new MyTask(4, "任務4");MyTask mt5 = new MyTask(5, "任務5");MyTask mt6 = new MyTask(6, "任務6");pool.execute(mt1);pool.execute(mt2);pool.execute(mt3);pool.execute(mt4);pool.execute(mt5);pool.execute(mt6);pool.shutdown();}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;public class UseThreadPoolExecutor2 implements Runnable{private static AtomicInteger count = new AtomicInteger(0);@Overridepublic void run() {try {int temp = count.incrementAndGet();System.out.println("任務" + temp);Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}public static void main(String[] args) throws Exception{//System.out.println(Runtime.getRuntime().availableProcessors());BlockingQueue<Runnable> queue =
// new LinkedBlockingQueue<Runnable>();new ArrayBlockingQueue<Runnable>(10);ExecutorService executor = new ThreadPoolExecutor(5, //core10, //max120L, //2fenzhongTimeUnit.SECONDS,queue);for(int i = 0 ; i < 20; i++){executor.execute(new UseThreadPoolExecutor2());}Thread.sleep(1000);System.out.println("queue size:" + queue.size()); //10Thread.sleep(2000);}}
public class MyTask implements Runnable {private int taskId;private String taskName;public MyTask(int taskId, String taskName){this.taskId = taskId;this.taskName = taskName;}public int getTaskId() {return taskId;}public void setTaskId(int taskId) {this.taskId = taskId;}public String getTaskName() {return taskName;}public void setTaskName(String taskName) {this.taskName = taskName;}@Overridepublic void run() {try {System.out.println("run taskId =" + this.taskId);Thread.sleep(5*1000);//System.out.println("end taskId =" + this.taskId);} catch (InterruptedException e) {e.printStackTrace();} }public String toString(){return Integer.toString(this.taskId);}}
?
import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor;public class MyRejected implements RejectedExecutionHandler{public MyRejected(){}@Overridepublic void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {System.out.println("自定義處理..");System.out.println("當前被拒絕任務為:" + r.toString());}}?
總結
以上是生活随笔為你收集整理的线程池 Executors2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Queue讲解
- 下一篇: Disruptor并发框架-1