| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,97 @@ | |||
| 1 | + package com.winterbe.java8.samples.concurrent; | ||
| 2 | + | ||
| 3 | + import java.util.Arrays; | ||
| 4 | + import java.util.List; | ||
| 5 | + import java.util.concurrent.Callable; | ||
| 6 | + import java.util.concurrent.ExecutionException; | ||
| 7 | + import java.util.concurrent.ExecutorService; | ||
| 8 | + import java.util.concurrent.Executors; | ||
| 9 | + import java.util.concurrent.ScheduledExecutorService; | ||
| 10 | + import java.util.concurrent.ScheduledFuture; | ||
| 11 | + import java.util.concurrent.TimeUnit; | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * @author Benjamin Winterberg | ||
| 15 | + */ | ||
| 16 | + public class Executors3 { | ||
| 17 | + | ||
| 18 | + public static void main(String[] args) throws InterruptedException, ExecutionException { | ||
| 19 | + // test1(); | ||
| 20 | + // test2(); | ||
| 21 | + // test3(); | ||
| 22 | + | ||
| 23 | + // test4(); | ||
| 24 | + test5(); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + private static void test5() throws InterruptedException, ExecutionException { | ||
| 28 | + ExecutorService executor = Executors.newWorkStealingPool(); | ||
| 29 | + | ||
| 30 | + List<Callable<String>> callables = Arrays.asList( | ||
| 31 | + callable("task1", 2), | ||
| 32 | + callable("task2", 1), | ||
| 33 | + callable("task3", 3)); | ||
| 34 | + | ||
| 35 | + String result = executor.invokeAny(callables); | ||
| 36 | + System.out.println(result); | ||
| 37 | + | ||
| 38 | + executor.shutdown(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + private static Callable<String> callable(String result, long sleepSeconds) { | ||
| 42 | + return () -> { | ||
| 43 | + TimeUnit.SECONDS.sleep(sleepSeconds); | ||
| 44 | + return result; | ||
| 45 | + }; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + private static void test4() throws InterruptedException { | ||
| 49 | + ExecutorService executor = Executors.newWorkStealingPool(); | ||
| 50 | + | ||
| 51 | + List<Callable<String>> callables = Arrays.asList( | ||
| 52 | + () -> "task1", | ||
| 53 | + () -> "task2", | ||
| 54 | + () -> "task3"); | ||
| 55 | + | ||
| 56 | + executor.invokeAll(callables) | ||
| 57 | + .stream() | ||
| 58 | + .map(future -> { | ||
| 59 | + try { | ||
| 60 | + return future.get(); | ||
| 61 | + } | ||
| 62 | + catch (Exception e) { | ||
| 63 | + throw new IllegalStateException(e); | ||
| 64 | + } | ||
| 65 | + }) | ||
| 66 | + .forEach(System.out::println); | ||
| 67 | + | ||
| 68 | + executor.shutdown(); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + private static void test3() { | ||
| 72 | + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); | ||
| 73 | + executor.scheduleWithFixedDelay(() -> { | ||
| 74 | + try { | ||
| 75 | + TimeUnit.SECONDS.sleep(2); | ||
| 76 | + System.out.println("Scheduling: " + System.nanoTime()); | ||
| 77 | + } | ||
| 78 | + catch (InterruptedException e) { | ||
| 79 | + System.err.println("task interrupted"); | ||
| 80 | + } | ||
| 81 | + }, 0, 1, TimeUnit.SECONDS); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + private static void test2() { | ||
| 85 | + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); | ||
| 86 | + executor.scheduleAtFixedRate(() -> System.out.println("Scheduling: " + System.nanoTime()), 0, 1, TimeUnit.SECONDS); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + private static void test1() throws InterruptedException { | ||
| 90 | + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); | ||
| 91 | + ScheduledFuture<?> future = executor.schedule(() -> System.out.println("Scheduling: " + System.nanoTime()), 3, TimeUnit.SECONDS); | ||
| 92 | + TimeUnit.MILLISECONDS.sleep(1337); | ||
| 93 | + long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS); | ||
| 94 | + System.out.printf("Remaining Delay: %sms\n", remainingDelay); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments