Java-五種線程池,四種拒絕策略,三種阻塞隊列(轉)


Java-五種線程池,四種拒絕策略,三種阻塞隊列

三種阻塞隊列:
    BlockingQueue<Runnable> workQueue = null;
    workQueue = new ArrayBlockingQueue<>(5);//基於數組的先進先出隊列,有界
    workQueue = new LinkedBlockingQueue<>();//基於鏈表的先進先出隊列,無界
    workQueue = new SynchronousQueue<>();//無緩沖的等待隊列,無界
四種拒絕策略:
    RejectedExecutionHandler rejected = null;
    rejected = new ThreadPoolExecutor.AbortPolicy();//默認,隊列滿了丟任務拋出異常
    rejected = new ThreadPoolExecutor.DiscardPolicy();//隊列滿了丟任務不異常
    rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//將最早進入隊列的任務刪,之后再嘗試加入隊列
    rejected = new ThreadPoolExecutor.CallerRunsPolicy();//如果添加到線程池失敗,那么主線程會自己去執行該任務
五種線程池:
    ExecutorService threadPool = null;
    threadPool = Executors.newCachedThreadPool();//有緩沖的線程池,線程數 JVM 控制
    threadPool = Executors.newFixedThreadPool(3);//固定大小的線程池
    threadPool = Executors.newScheduledThreadPool(2);
    threadPool = Executors.newSingleThreadExecutor();//單線程的線程池,只有一個線程在工作
    threadPool = new ThreadPoolExecutor();//默認線程池,可控制參數比較多   

public static void main (String[] args) throws Exception {
    testThreadPoolExecutor();
}
public static void testThreadPoolExecutor() throws Exception {
    //基礎參數
    int corePoolSize=2;//最小活躍線程數
    int maximumPoolSize=5;//最大活躍線程數
    int keepAliveTime=5;//指定線程池中線程空閑超過 5s 后將被回收
    TimeUnit unit = TimeUnit.SECONDS;//keepAliveTime 單位
    //阻塞隊列
    BlockingQueue<Runnable> workQueue = null;
    workQueue = new ArrayBlockingQueue<>(5);//基於數組的先進先出隊列,有界
    workQueue = new LinkedBlockingQueue<>();//基於鏈表的先進先出隊列,無界
    workQueue = new SynchronousQueue<>();//無緩沖的等待隊列,無界
    //拒絕策略
    RejectedExecutionHandler rejected = null;
    rejected = new ThreadPoolExecutor.AbortPolicy();//默認,隊列滿了丟任務拋出異常
    rejected = new ThreadPoolExecutor.DiscardPolicy();//隊列滿了丟任務不異常
    rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//將最早進入隊列的任務刪,之后再嘗試加入隊列
    rejected = new ThreadPoolExecutor.CallerRunsPolicy();//如果添加到線程池失敗,那么主線程會自己去執行該任務
    //使用的線程池
    ExecutorService threadPool = null;
    threadPool = Executors.newCachedThreadPool();//有緩沖的線程池,線程數 JVM 控制
    threadPool = Executors.newFixedThreadPool(3);//固定大小的線程池
    threadPool = Executors.newScheduledThreadPool(2);
    threadPool = Executors.newSingleThreadExecutor();//單線程的線程池,只有一個線程在工作
    threadPool = new ThreadPoolExecutor(
            corePoolSize,
            maximumPoolSize,
            keepAliveTime,
            unit,
            workQueue,
            rejected);//默認線程池,可控制參數比較多
    //執行無返回值線程
    TaskRunnable taskRunnable = new TaskRunnable();
    threadPool.execute(taskRunnable);
    List<Future<String>> futres = new ArrayList<>();
    for(int i=0;i<10;i++) {
        //執行有返回值線程
        TaskCallable taskCallable = new TaskCallable(i);
        Future<String> future = threadPool.submit(taskCallable);
        futres.add(future);
    }
    for(int i=0;i<futres.size();i++){
        String result = futres.get(i).get();
        System.out.println(i+" result = "+result);
    }
}
/**
    * 返回值的線程,使用 threadpool.execut() 執行
    */
public static class TaskRunnable implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " runnable result!");
    }
}
/**
    * 有返回值的線程,使用 threadpool.submit() 執行
    */
public static class TaskCallable implements Callable<String>{
    public TaskCallable(int index){
        this.i=index;
    }
    private int i;
    @Override
    public String call() throws Exception {
        int r = new Random().nextInt(5);
        try {
            Thread.sleep(r);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //System.out.println("callable result!");
        return Thread.currentThread().getName()+" callable index="+i +",sleep="+r;
    }
}

 

————————————————
版權聲明:本文為CSDN博主「草青工作室」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/xxj_jing/article/details/84835476


免責聲明!

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



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