Javaのスレッドを使用--ThreadPoolExecutor

オリジナル:転載は元のアドレスを示すhttps://www.cnblogs.com/fanerwei222/p/11871132.html

Javaのスレッドを使用--ThreadPoolExecutor

パブリック 静的 ボイドメイン(文字列[]引数){ 
        ExecutorServiceのサービス = 新しい ThreadPoolExecutor(1、2、1000 TimeUnit.MILLISECONDS、新しい SynchronousQueue <Runnableを>()、Executors.defaultThreadFactory()、新しいThreadPoolExecutor.AbortPolicy())。
        以下のためにINT I = 0; I <3; I ++ ){ 
            service.execute(新しいThreadPoolExecutorTask())。
        } 
    }
/ ** 
 *タスク実行スレッドプール
 * / 
クラス ThreadPoolExecutorTask 実装Runnableを{ 

    @Override 
    公共 ボイドRUN(){ 
        System.out.printlnは(にThread.currentThread()のgetName()); 
    } 
}

次のように印刷結果は以下のとおりです。

スレッドの例外「メイン」java.util.concurrent.RejectedExecutionException:= 2、アクティブなスレッド= 2タスクconcurrent.ThreadPoolExecutorTask@6d6f6e28を実行している[java.util.concurrent.ThreadPoolExecutor@135fbaa4から拒否され、プールサイズ、完了= 0キューイングされたタスク、タスク= 0 ] 
    :java.util.concurrent.ThreadPoolExecutor $ AbortPolicy.rejectedExecution(ThreadPoolExecutor.javaで 2063 
    :java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.javaで 830 
    java.util.concurrent.ThreadPoolExecutor.executeで(ThreadPoolExecutor.java: 1379 
    concurrent.MainConcurrent.mainで(MainConcurrent.java: 37 
プール -1-スレッド1 
プール-1スレッド2

そして、それを見ることができる、スレッドプールの設定の最大サイズを超えて、例外が報告されます!

しかし、それは他のパラメータを導入ありません。

おすすめ

転載: www.cnblogs.com/fanerwei222/p/11871132.html