亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java線(xiàn)程池使用AbortPolicy策略

 更新時(shí)間:2022年06月30日 08:46:39   作者:??Gxin????  
這篇文章主要介紹了?Java線(xiàn)程池使用AbortPolicy策略,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下

線(xiàn)程池ThreadPoolExecutor的拒絕策略

線(xiàn)程池中的線(xiàn)程資源全部被占用時(shí),對(duì)新添加的Task任務(wù)有不同的處理策略,在默認(rèn)的情況下,

ThreadPoolExecutor類(lèi)中有4種不同的處理方式:

  • AbortPolicy:當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),它將拋出RejectExecutionException異常。
  • CallerRunsPolicy:當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),會(huì)使用調(diào)用線(xiàn)程池的Thread線(xiàn)程對(duì)象處理被拒絕的任務(wù)。
  • DiscardOldestPolicy: 當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),線(xiàn)程池會(huì)放棄等待隊(duì)列中最舊的未處理任務(wù),然后將被拒絕的任務(wù)添加到等待隊(duì)列中。
  • DiscardPolicy:當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),線(xiàn)程池將丟棄被拒絕的任務(wù)。

AbortPolicy策略

AbortPolicy策略是當(dāng)任務(wù)添加到線(xiàn)程池中被拒絕時(shí),它將拋出RejectedExecutionException異常。

線(xiàn)程執(zhí)行代碼如下:

public class FirstRunnable implements Runnable {
    @Override
    public void run() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        try {
            System.out.println(Thread.currentThread().getName() +"  開(kāi)始時(shí)間:"+simpleDateFormat.format(new Date()));
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() +"  結(jié)束時(shí)間:"+simpleDateFormat.format(new Date()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

運(yùn)行類(lèi)代碼如下:

public class AbortPolicyRun {
    public static void main(String[] args) {
        FirstRunnable firstRunnable = new FirstRunnable();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 7 ; i++) {
            threadPoolExecutor.execute(firstRunnable);
        }
    }
}

運(yùn)行結(jié)果如下:

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.ozx.concurrentprogram.executor.service.FirstRunnable@6c629d6e rejected from java.util.concurrent.ThreadPoolExecutor@5f5a92bb[Running, pool size = 3, active threads = 3, queued tasks = 2, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
    at com.ozx.concurrentprogram.executor.controller.AbortPolicyRun.main(AbortPolicyRun.java:19)
pool-1-thread-3  開(kāi)始時(shí)間:16:20:27
pool-1-thread-1  開(kāi)始時(shí)間:16:20:27
pool-1-thread-2  開(kāi)始時(shí)間:16:20:27
pool-1-thread-2  結(jié)束時(shí)間:16:20:28
pool-1-thread-2  開(kāi)始時(shí)間:16:20:28
pool-1-thread-1  結(jié)束時(shí)間:16:20:28
pool-1-thread-1  開(kāi)始時(shí)間:16:20:28
pool-1-thread-3  結(jié)束時(shí)間:16:20:28
pool-1-thread-1  結(jié)束時(shí)間:16:20:29
pool-1-thread-2  結(jié)束時(shí)間:16:20:29

使用AbortPolicy策略后,線(xiàn)程任務(wù)數(shù)量超出線(xiàn)程池最大線(xiàn)程數(shù)時(shí),線(xiàn)程池將拋出java.util.concurrent.RejectedExecutionException異常。

到此這篇關(guān)于 Java線(xiàn)程池使用AbortPolicy策略的文章就介紹到這了,更多相關(guān)Java AbortPolicy內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論