Javaスレッドプールのいくつかの実装方法と違いの紹介
java.text.dateformat;インポートjava.text.simpledateformat; Import java.util.arraylist; Import java.util.date; Import java.util.list; Import java.util.list; Import Java.util.util.concurrent.callable; import java.util.util.util.util.util.util.util.util.util.curerent java.util.concurrent.executors; import java.util.concurrent.future; public class testthreadpool {//-newfixedthreadpoolはcachethreadpoolに似ており、再利用する場合は使用できますが、いつでも順調に順番に、新しいスレッドを作成することはできません。この時点で、新しいスレッドが確立される場合、別のキューに配置し、現在のスレッドのスレッドがプールから直接移動するまで待つことができます// - Cachethreadpoolとは異なり、Threadpoolにはアイドルメカニズムがありません(それはそこにあるかもしれませんが、ドキュメントはそれを言及していません)固定されたThreadpoolは、主にいくつかの非常に安定した固定された通常の同時スレッドを対象としており、主にサーバーで使用されています//-メソッドのソースコード、キャッシュプールと固定プールは同じ基礎プールを呼び出しますが、パラメーターは異なりますが、固定プールスレッド数は固定されています。まったく考慮)、60秒のアイドルprivate static executorservice sixedservice = executors.newfixedthreadpool(6); // -cacheプール、最初にプールに以前に確立されたスレッドがあるかどうかを確認します。そうでない場合は、新しいスレッドを作成してプールに追加します// -cacheプールは通常、非常に短い寿命の非同期タスクを実行するために使用されます。したがって、接続指向のデーモン型サーバーではあまり使用されません。 //-再利用できるスレッドは、タイムアウトアイドル内のプール内のスレッドでなければなりません。デフォルトのタイムアウトは60年代です。アイドル時間が期間を超えると、スレッドインスタンスが終了し、プールから移動します。 // cachedthreadpoolに入れるスレッドは、その終わりを心配する必要がないことに注意してください。タイムアウト後に非アクティブな場合、自動的に終了します。 private static executorservice cacheservice = executors.newcachedthreadpool(); // -Singletonスレッド、いつでも1つのスレッドしかありません// -scheduledスレッドプール// - このプールのスレッドは、スケジュールまたはプライベートStatic executorservice scheduledservice = executors.newscheduledthreadpool(10)に従って順番に遅延することができます。 public static void main(string [] args){dateformat format = new simpledateFormat( "yyyy-mm-dd hh:mm:ss"); List <Integer> CustomerList = new ArrayList <Integer>(); system.out.println(format.format(new date())); testfixedthreadpool(fixedservice、customerlist); System.out.println( "-----------------------"); testfixedthreadpool(fixedservice、customerlist); fixedservice.shutdown(); system.out.println(fixedservice.isshutdown()); System.out.println( "-----------------------------------------------"); testCachethReadPool(Cacheservice、CustomerList); System.out.println( "-----------------------------------------------"); testCachethReadPool(Cacheservice、CustomerList); cacheservice.shutdownnow(); System.out.println( "-----------------------------------------------"); testsingleserviceThreadPool(Singleservice、CustomerList); testsingleserviceThreadPool(Singleservice、CustomerList); singleservice.shutdown(); System.out.println( "-----------------------------------------------"); testscheduledserviceThreadPool(ScheduledService、CustomerList); testscheduledserviceThreadPool(ScheduledService、CustomerList); ScheduleDservice.shutdown(); } public static void testscheduledservicethreadpool(executorservice service、list <integer> customerlist){list <callable <integer >> listcallable = new arraylist <callable <integer >>(); for(int i = 0; i <10; i ++){callable <integer> callable = new callable <integer>(){@override public integer call()throws exception {return new Random()。nextint(10); }}; listCallable.Add(callable); } try {list <future <integer >> listfuture = service.invokeall(listCallable); for(future <integer> future:listfuture){integer id = future.get(); customerlist.add(id); }} catch(例外e){e.printstacktrace(); } system.out.println(customerlist.toString()); } public static void testSingleserviceThreadPool(executorService Service、list <integer> customerlist){list <callable <list <integer >>>> listcallable = new arraylist <callable <integer >>>(); for(int i = 0; i <10; i ++){callable <list <integer >>> callable = new callable <list <integer >>(){@override public list <integer> call()throws {list <integer> list = getlist(new Random()。 boolean isstop = false; while(list.size()> 0 &&!isstop){system.out.println(thread.currentthread()。getId() + " - sleep:1000"); isStop = true; }返品リスト。 }}; listCallable.Add(callable); } try {list <future <list <integer >>> listfuture = service.invokeall(listcallable); for(future <list <integer >> future:listfuture){list <integer> list = future.get(); customerlist.addall(list); }} catch(例外e){e.printstacktrace(); } system.out.println(customerlist.toString()); } public static void testcachethreadpool(executorservice service、list <integer> customerlist){list <callable <list <integer >>> listcallable = new arrayList <callable <integer >>(); for(int i = 0; i <10; i ++){callable <list <integer >> callable = new callable <list <integer >>(){@override public list <integer> call()throws {list <integer> list = getList(new Random()。 boolean isstop = false; while(list.size()> 0 &&!isstop){system.out.println(thread.currentthread()。getId() + " - sleep:1000"); isStop = true; }返品リスト。 }}; listCallable.Add(callable); } try {list <future <list <integer >>> listfuture = service.invokeall(listcallable); for(future <list <integer >> future:listfuture){list <integer> list = future.get(); customerlist.addall(list); }} catch(例外e){e.printstacktrace(); } system.out.println(customerlist.toString()); } public static void testfixedthreadpool(executorservice service、list <integer> customerlist){list <callable <list <integer >>> listcallable = new arraylist <callable <integer >>>(); for(int i = 0; i <10; i ++){callable <list <integer >> callable = new callable <list <integer >>(){@override public list <integer> call()throws {list <integer> list = getList(new Random()。 boolean isstop = false; while(list.size()> 0 &&!isstop){system.out.println(thread.currentthread()。getId() + " - sleep:1000"); isStop = true; }返品リスト。 }}; listCallable.Add(callable); } try {list <future <list <integer >>> listfuture = service.invokeall(listcallable); for(future <list <integer >> future:listfuture){list <integer> list = future.get(); customerlist.addall(list); }} catch(例外e){e.printstacktrace(); } system.out.println(customerlist.toString()); } public static list <integer> getList(int x){list <integer> list = new ArrayList <Integer>(); list.add(x); list.add(x * x);返品リスト。 }}使用:LinkedBlockingQueueでスレッドプールの説明を実装します
例:corepoolsize = 3、maximummumpoolsize = 6、linkedblockingqueue(10)// redjededexecutionhandlerデフォルト処理方法は:threadpoolexecutor.abortpolicy // shoodpoolexecutor executorservice = new swreetpoolexecutor(corepoolsize、maximumpoolsize、1l、1L、タイムナイト、Time.seconds、New seconds、neg LinkedBlockingQueue <Runnable>(10)); // 1。スレッドプールで実行されているスレッド(つまり、executorservice.executeを呼び出す)がlinkedblockingqueue.init(10)に到達しない場合、現在実行されているスレッドの数はcorepoolsize(3)// 2です。スレッドの数がlinkedblockingqueue.(10)を超えて> = init(10)+corepoolsize(3)を超えており、init(10)+maximumpoolsizeよりも少ない場合。現在開始されているスレッドの数は次のとおりです。(現在のスレッドInit(10))// 3。呼び出されたスレッドの数がinit(10)+maximumpoolsizeを超える場合、これは拒否executionhandlerのルールに従って処理されます。
概要:RejectedExecutionHandlerのいくつかのデフォルト実装の説明
//デフォルトが使用されています:threadpoolexecutor.abortpolicy。ハンドラーは拒否され、ランタイム拒否ExecutionExceptionを投げます。 RejectedExecutionHandler Policy = new SthreadPoolexecutor.abortPolicy(); //// threadPoolexecutor.callerrunSpolicyのin Threadは、タスクを実行する実行自体を呼び出します。この戦略は、新しいタスクの提出を遅らせる可能性のある簡単なフィードバック制御メカニズムを提供します。 // policy = new SthreadPoolexecutor.callerrunspolicy(); //// threadpoolexecutor.discardpolicyで、実行できないタスクは削除されます。 // policy = new SthreadPoolexecutor.DiscardPolicy(); //// threadPoolexecutor.discardoldestPolicyで、執行者が閉じられていない場合、作業キューのヘッドでのタスクが削除され、プログラムの実行が再試行されます(再び失敗した場合、このプロセスを繰り返します)。 // policy = new SthreadPoolexecutor.discardoldestpolicy();
Javaスレッドプールの実装方法と違いの上記の紹介は、私があなたと共有したすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。