Java ScheduledExecutorServiceの例
Javaでは、
ScheduledExecutorService`を使用して、タスクを定期的に、または
TimeUnit`によって定義済みの遅延の後に1回実行することができます。
1.タスクを1回実行する
ScheduledExecutorService`は
Runnable`と `Callable`タスクの両方を受け入れます。
1.1 5秒の初期遅延後に `Runnable`タスクを実行します。
ScheduledExecutorExample.java
package com.mkyong.concurrency.executor.scheduler; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ScheduledExecutorRunnable { public static void main(String[]args) { ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); Runnable task2 = () -> System.out.println("Running task2..."); task1(); //run this task after 5 seconds, nonblock for task3 ses.schedule(task2, 5, TimeUnit.SECONDS); task3(); ses.shutdown(); } public static void task1() { System.out.println("Running task1..."); } public static void task3() { System.out.println("Running task3..."); } }
出力
Running task1... Running task3... Running task2...//display after 5 seconds
1.2 5秒の初期遅延後に `Callable`タスクを実行します。
ScheduledExecutorExample.java
package com.mkyong.concurrency.executor.scheduler; import java.util.concurrent.** ; public class ScheduledExecutorCallable { public static void main(String[]args) throws InterruptedException, ExecutionException { ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); Callable<Integer> task2 = () -> 10; task1(); //run this task after 5 seconds, nonblock for task3, returns a future ScheduledFuture<Integer> schedule = ses.schedule(task2, 5, TimeUnit.SECONDS); task3(); //block and get the result System.out.println(schedule.get()); System.out.println("shutdown!"); ses.shutdown(); } public static void task1() { System.out.println("Running task1..."); } public static void task3() { System.out.println("Running task3..."); } }
出力
Running task1... Running task3... 10 //display after 5 seconds shutdown!
2.定期的にタスクを実行する
定期的にタスクを実行するには、 `scheduleAtFixedRate`を使います。
2.1 5秒の初期遅延の後、1秒ごとに `Runnable`タスクを実行します。
ScheduledExecutorExample.java
package com.mkyong.concurrency.executor.scheduler; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; public class ScheduledExecutorRepeat { private static int count = 0; public static void main(String[]args) throws InterruptedException { ScheduledExecutorService ses = Executors.newScheduledThreadPool(1); Runnable task1 = () -> { count++; System.out.println("Running...task1 - count : " + count); }; //init Delay = 5, repeat the task every 1 second ScheduledFuture<?> scheduledFuture = ses.scheduleAtFixedRate(task1, 5, 1, TimeUnit.SECONDS); while (true) { System.out.println("count :" + count); Thread.sleep(1000); if (count == 5) { System.out.println("Count is 5, cancel the scheduledFuture!"); scheduledFuture.cancel(true); ses.shutdown(); break; } } } }
出力
count :0 count :0 count :0 count :0 count :0 Running...task1 - count : 1 count :1 Running...task1 - count : 2 count :2 Running...task1 - count : 3 count :3 Running...task1 - count : 4 count :4 Running...task1 - count : 5 Count is 5, cancel the scheduledFuture!
ソースコードをダウンロードする
$ git clone
https://github.com/mkyong/java-concurrency.git
参考文献
-
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html
[ScheduledExecutorService
JavaDoc]