线程池 Executors2
生活随笔
收集整理的這篇文章主要介紹了
线程池 Executors2
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;public class UseThreadPoolExecutor1 {public static void main(String[] args) {/*** 在使用有界隊(duì)列時(shí),若有新的任務(wù)需要執(zhí)行,如果線程池實(shí)際線程數(shù)小于corePoolSize,則優(yōu)先創(chuàng)建線程,* 若大于corePoolSize,則會(huì)將任務(wù)加入隊(duì)列,* 若隊(duì)列已滿,則在總線程數(shù)不大于maximumPoolSize的前提下,創(chuàng)建新的線程,* 若線程數(shù)大于maximumPoolSize,則執(zhí)行拒絕策略。或其他自定義方式。* */ ThreadPoolExecutor pool = new ThreadPoolExecutor(1, //coreSize2, //MaxSize60, //60TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3) //指定一種隊(duì)列 (有界隊(duì)列)//new LinkedBlockingQueue<Runnable>(), new MyRejected()
// , new DiscardOldestPolicy());MyTask mt1 = new MyTask(1, "任務(wù)1");MyTask mt2 = new MyTask(2, "任務(wù)2");MyTask mt3 = new MyTask(3, "任務(wù)3");MyTask mt4 = new MyTask(4, "任務(wù)4");MyTask mt5 = new MyTask(5, "任務(wù)5");MyTask mt6 = new MyTask(6, "任務(wù)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("任務(wù)" + 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("當(dāng)前被拒絕任務(wù)為:" + r.toString());}}?
總結(jié)
以上是生活随笔為你收集整理的线程池 Executors2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Queue讲解
- 下一篇: Disruptor并发框架-1