blog

ScheduledThreadPool

... ......

Jun 14, 2020 · 5 min. read
シェア

ScheduleThreadPoolの作成

ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);

ここで、または自動的に作成することを選択し、パラメータは5であるcorePoolSize、以下は自動的に作成する方法を参照してください。

public ScheduledThreadPoolExecutor(int corePoolSize) {
 super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
 new DelayedWorkQueue());
 }

ソース・コードからわかるように、次のように親クラスのコンストラクタ・メソッドを呼び出してスレッド・プールを作成しています。

public class ScheduledThreadPoolExecutor
 extends ThreadPoolExecutor
 implements ScheduledExecutorService
  • 最初のパラメータはcorePoolSIzeで、5コアのスレッド数です。
  • 2番目のパラメータであるmaximumPoolSizeはInteger.MAX_VALUEで、作成可能なスレッドの理論上の最大数を表します。
  • 3番目のパラメータの位置は、KeepAliveTime、つまりアイドルスレッドの生存時間です。
  • 4つ目のパラメータの位置は、単位ですが、実際にはNANOSECONDSで、とにかく、KeepAliveTimeの前が0で、ナノ秒かミリ秒かで、ほぼ同じようです。
  • 5番目のパラメータであるHandlerは、DelayedWorkQueue(遅延キュー)です。

ScheduleThreadPoolの使用

ScheduledThreadPool.shedule(Runnable command, long delay, TimeUnit unit)

public class TestScheduledThreadPool {
 public static void main(String[] args) {
 ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);
 Thread thread = new Thread(new Runnable() {
 @Override
 public void run() {
 System.out.println(Thread.currentThread().getName() + " ," +
 "現在の時間は " + LocalDateTime.now());
 }
 });
 for (int i = 0; i < 10; ++i) {
 try {
 threadPool.schedule(thread, 5l, TimeUnit.SECONDS);
 Thread.sleep(1000);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 threadPool.shutdown();
 }
}

2番目のパラメーターは、タスクの実行を遅延させる時間です。3番目のパラメーターは、遅延時間の単位です。

また、上記のタスクを一度実行した後、スレッドを1秒スリープさせ、再度タスクを投入したところ、当然のことながら、タスクが投入されるたびに5秒間隔で実行されました。

scheduleAtFixedRate(実行可能コマンド, long initialDelay, long period, TimeUnit unit)

public class TestScheduledThreadPool {
 public static void main(String[] args) {
 ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);
 Thread thread = new Thread(new Runnable() {
 @Override
 public void run() {
 System.out.println(Thread.currentThread().getName() + " ," +
 "現在の時間は " + LocalDateTime.now());
 }
 });
 threadPool.scheduleAtFixedRate(thread, 5l, 2l, TimeUnit.SECONDS);
 }
}

最初のパラメータは、提出したいタスクです 2番目のパラメータは、実行を開始する最初のタスクを初期化した後、何秒です 3番目のパラメータは、期間、つまり、単位は長いです、あなたはどのくらいの時間を設定します 毎回です 4番目のパラメータは、時間の単位です 時間の単位の前の2番目と3番目のパラメータについては、私はここで秒です

(プリントアウトの時間内容が示すように、2秒ごとにタスクに移動し、メソッドは停止しません)

つまり、scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)は、最初の試行時間の後、タスクの開始を呼び出すたびに時間が計測され、このタスクが終了するかどうかがカウントダウンされ、新しいタスクの実行に移ります。

  • 最初の試行時間を5、期間を2、すなわち0:00としてタスクをコール。
  • 0:05 最初のミッション開始
  • 0:07 第2ミッション開始
  • 0:09 第2ミッション開始
  • ...

scheduleWithFixedDelay(Runnableコマンド、ong initialDelay、long delay、TimeUnit unit)。

public class TestScheduledThreadPool {
 public static void main(String[] args) {
 ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);
 Thread thread = new Thread(new Runnable() {
 @Override
 public void run() {
 System.out.println(Thread.currentThread().getName() + " ," +
 "現在の時間は " + LocalDateTime.now());
 }
 });
 threadPool.scheduleWithFixedDelay(thread, 5l, 4l, TimeUnit.SECONDS);
 }
}
  • 最初の試行時間を5、期間を4、すなわち0:00としてタスクをコール。
  • 最初のミッション開始時刻は0:05、ミッション終了時刻は0:08(ランダム表記)
  • 2回目のミッション開始は0:12、ミッション終了は0:17(ランダム表記)
  • 第3ミッションの開始は0:21、終了は0:24(ランダム表記)
  • ...つまり、そのサイクルは前のスレッドが終了したときにカウントダウンを開始します。

submit(実行可能タスク)

public class TestScheduledThreadPool {
 public static void main(String[] args) {
 ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);
 Thread thread = new Thread(new Runnable() {
 @Override
 public void run() {
 System.out.println(Thread.currentThread().getName() + " ," +
 "現在の時間は " + LocalDateTime.now());
 }
 });
 for (int i = 0; i < 10; ++i) {
 threadPool.submit(thread);
 }
 threadPool.shutdown();
 }
}

他のスレッドプールでは、ここでsubmit()で、違いはないように思われ、直接タスクに提出され、もちろん、ビューのソースコードの点から、書かれたコードが異なっていることがわかります。

public Future<?> submit(Runnable task) {
 return schedule(task, 0, NANOSECONDS);
 }

2番目のパラメータはdelay、つまり0です。これはタスクが投入され、何秒待つかを遅延させることなく実行されることを意味します。

Read next

Vueの初期化処理(a)

Vueをよく使うフロントエンドの学生にとって、以下のコード行はとても見慣れたものですが、このコード行が実際に何をしているのかはあまり明確ではありません。 そこで次に、ソースコードを通して、Vueの初期化の全体的なプロセスを段階的に分析します。Vue版本@2.6.x,编译版本。 Vueは基本的にコンストラクタ、new Vue ()操作の実装であることがわかります。

Jun 14, 2020 · 9 min read