From 28e578227501a63c61b8ac8ad642508191933fc6 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Fri, 27 Mar 2015 11:19:03 +0100 Subject: [PATCH] Scheduled executors --- .../java8/samples/concurrent/Executors3.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/com/winterbe/java8/samples/concurrent/Executors3.java diff --git a/src/com/winterbe/java8/samples/concurrent/Executors3.java b/src/com/winterbe/java8/samples/concurrent/Executors3.java new file mode 100644 index 0000000..8712ee3 --- /dev/null +++ b/src/com/winterbe/java8/samples/concurrent/Executors3.java @@ -0,0 +1,97 @@ +package com.winterbe.java8.samples.concurrent; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class Executors3 { + + public static void main(String[] args) throws InterruptedException, ExecutionException { +// test1(); +// test2(); +// test3(); + +// test4(); + test5(); + } + + private static void test5() throws InterruptedException, ExecutionException { + ExecutorService executor = Executors.newWorkStealingPool(); + + List> callables = Arrays.asList( + callable("task1", 2), + callable("task2", 1), + callable("task3", 3)); + + String result = executor.invokeAny(callables); + System.out.println(result); + + executor.shutdown(); + } + + private static Callable callable(String result, long sleepSeconds) { + return () -> { + TimeUnit.SECONDS.sleep(sleepSeconds); + return result; + }; + } + + private static void test4() throws InterruptedException { + ExecutorService executor = Executors.newWorkStealingPool(); + + List> callables = Arrays.asList( + () -> "task1", + () -> "task2", + () -> "task3"); + + executor.invokeAll(callables) + .stream() + .map(future -> { + try { + return future.get(); + } + catch (Exception e) { + throw new IllegalStateException(e); + } + }) + .forEach(System.out::println); + + executor.shutdown(); + } + + private static void test3() { + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + executor.scheduleWithFixedDelay(() -> { + try { + TimeUnit.SECONDS.sleep(2); + System.out.println("Scheduling: " + System.nanoTime()); + } + catch (InterruptedException e) { + System.err.println("task interrupted"); + } + }, 0, 1, TimeUnit.SECONDS); + } + + private static void test2() { + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + executor.scheduleAtFixedRate(() -> System.out.println("Scheduling: " + System.nanoTime()), 0, 1, TimeUnit.SECONDS); + } + + private static void test1() throws InterruptedException { + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + ScheduledFuture future = executor.schedule(() -> System.out.println("Scheduling: " + System.nanoTime()), 3, TimeUnit.SECONDS); + TimeUnit.MILLISECONDS.sleep(1337); + long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS); + System.out.printf("Remaining Delay: %sms\n", remainingDelay); + } + +} -- GitLab