序文
前のSpring Boot Basicチュートリアルシリーズでは、「@Asyncを使用してSpring Bootに非同期コールを実装する」記事が@Asyncアノテーションを使用して非同期コールを実装する方法を紹介しました。ただし、これらの非同期実行を制御することは、私たち自身のアプリケーションの健康を確保するための基本的なスキルです。この記事では、カスタムスレッドプールを介した非同期呼び出しの同時性を制御するかどうかを学びましょう。
この記事の例を以前の例に基づいて変更したり、新しいSpring Bootプロジェクトを作成して試してみることができます。
スレッドプールを定義します
最初のステップは、次のようなスプリングブートメインクラスのスレッドプールを定義することです。
@springBootApplicationPublic class Application {public static void main(string [] args){springApplication.run(application.class、args); } @enableasync @configuration class taskpoolconfig {@bean( "taskexecutor")パブリックエグゼクティブtaskexecutor(){shoodpooltaskexecutor executor = new SthreadPooltaskexecutor(); executor.setCorepoolsize(10); executor.setmaxpoolsize(20); executor.setqueuecapacity(200); executor.setKeepaliveseConds(60); executor.setThreadNamePrefix( "taskexecutor-"); executor.setRejectedExecutionHandler(new ThreadPoolexecutor.CallerrunSpolicy());執行者を返す; }}}上記では、threadpooltaskexecutorを使用してスレッドプールを作成し、次のパラメーターを設定しました。
スレッドプールを使用します
スレッドプールを定義した後、このスレッドプールのリソースを使用して実行される非同期に呼ばれるタスクをどのように作成しますか?この方法は非常に簡単です。たとえば、 @asyncアノテーションでスレッドプール名を指定する必要があります。
@slf4j@componentpublic class task {public static random = new Random(); @async( "taskexecutor")public void dotaskone()throws exception {log.info( "start task one"); long start = system.currenttimemillis(); thread.sleep(random.nextint(10000)); long end = system.currenttimemillis(); log.info( "タスク1の完了、時間が取られた時間:" +(end -start) + "milliseconds"); } @async( "taskexecutor")public void dotasktwo()throws exception {log.info( "start task 2"); long start = system.currenttimemillis(); thread.sleep(random.nextint(10000)); long end = system.currenttimemillis(); log.info( "完全なタスク2、時間を取る時間:" +(end -start) + "milliseconds"); } @async( "taskexecutor")public void dotaskthree()throws exception {log.info( "start task 3"); long start = system.currenttimemillis(); thread.sleep(random.nextint(10000)); long end = system.currenttimemillis(); log.info( "タスク3の完了、時間消費:" +(end -start) + "milliseconds"); }}単体テスト
最後に、それを確認するための単位テストを書きましょう
@runwith(springjunit4classrunner.class)@springboottestpublic class applicationtests {@autowired private task task; @test public void test()throws Exception {task.dotaskone(); task.dotasktwo(); task.dotaskthree(); thread.currentthread()。join(); }}上記のユニットテストを実行すると、すべての出力スレッド名が以前に定義したスレッドプールによって前に付けられていることがわかります。つまり、スレッドプールを使用して非同期タスクを実行する実験が成功しました。
2018-03-27 22:01:15.620情報73703 --- [taskexecutor-1] com.didispace.async.task:タスク1を開始
2018-03-27 22:01:15.620情報73703 --- [taskexecutor-2] com.didispace.async.task:タスク2を開始
2018-03-27 22:01:15.620情報73703 --- [taskexecutor-3] com.didispace.async.task:タスク3を開始
2018-03-27 22:01:18.165 Info 73703 --- [taskexecutor-2] com.didispace.async.task:タスク2の完了、時間を取る時間:2545ミリ秒
2018-03-27 22:01:22.149 Info 73703 --- [taskexecutor-3] com.didispace.async.task:タスク3の完了、時間:6529ミリ秒
2018-03-27 22:01:23.912 Info 73703 --- [taskexecutor-1] com.didispace.async.task:タスク1を完了し、時間を取った時間:8292ミリ秒
完全な例:
読者は、選好に従って第4-1-1-3章プロジェクトを表示することを選択できます。
github:https://github.com/dyc87112/springboot-learning/
gitee:https://gitee.com/diidispace/springboot-learning/
ローカルダウンロード:http://xiazai.vevb.com/201805/yuanma/springboot-learning(vevb.com).rar
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。