| 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,45 @@ | |||
| 1 | + package com.winterbe.java8.samples.concurrent; | ||
| 2 | + | ||
| 3 | + import java.util.concurrent.ExecutorService; | ||
| 4 | + import java.util.concurrent.Executors; | ||
| 5 | + import java.util.concurrent.TimeUnit; | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * @author Benjamin Winterberg | ||
| 9 | + */ | ||
| 10 | + public class Executors1 { | ||
| 11 | + | ||
| 12 | + public static void main(String[] args) { | ||
| 13 | + test1(3); | ||
| 14 | + // test1(7); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + private static void test1(long seconds) { | ||
| 18 | + ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
| 19 | + executor.submit(() -> { | ||
| 20 | + try { | ||
| 21 | + TimeUnit.SECONDS.sleep(seconds); | ||
| 22 | + System.out.println("task finished: " + Thread.currentThread().getName()); | ||
| 23 | + } | ||
| 24 | + catch (InterruptedException e) { | ||
| 25 | + System.err.println("task interrupted"); | ||
| 26 | + } | ||
| 27 | + }); | ||
| 28 | + shutdown(executor); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + static void shutdown(ExecutorService executor) { | ||
| 32 | + try { | ||
| 33 | + System.out.println("attempt to shutdown executor"); | ||
| 34 | + executor.shutdown(); | ||
| 35 | + executor.awaitTermination(5, TimeUnit.SECONDS); | ||
| 36 | + } | ||
| 37 | + catch (InterruptedException e) { | ||
| 38 | + System.err.println("termination interrupted"); | ||
| 39 | + } | ||
| 40 | + finally { | ||
| 41 | + executor.shutdownNow(); | ||
| 42 | + System.out.println("shutdown finished"); | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,77 @@ | |||
| 1 | + package com.winterbe.java8.samples.concurrent; | ||
| 2 | + | ||
| 3 | + import java.util.concurrent.ExecutionException; | ||
| 4 | + import java.util.concurrent.ExecutorService; | ||
| 5 | + import java.util.concurrent.Executors; | ||
| 6 | + import java.util.concurrent.Future; | ||
| 7 | + import java.util.concurrent.TimeUnit; | ||
| 8 | + import java.util.concurrent.TimeoutException; | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * @author Benjamin Winterberg | ||
| 12 | + */ | ||
| 13 | + public class Executors2 { | ||
| 14 | + | ||
| 15 | + public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException { | ||
| 16 | + // test1(); | ||
| 17 | + // test2(); | ||
| 18 | + test3(); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + private static void test3() throws InterruptedException, ExecutionException, TimeoutException { | ||
| 22 | + ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
| 23 | + | ||
| 24 | + Future<Integer> future = executor.submit(() -> { | ||
| 25 | + try { | ||
| 26 | + TimeUnit.SECONDS.sleep(2); | ||
| 27 | + return 123; | ||
| 28 | + } | ||
| 29 | + catch (InterruptedException e) { | ||
| 30 | + throw new IllegalStateException("task interrupted", e); | ||
| 31 | + } | ||
| 32 | + }); | ||
| 33 | + | ||
| 34 | + future.get(1, TimeUnit.SECONDS); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + private static void test2() throws InterruptedException, ExecutionException { | ||
| 38 | + ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
| 39 | + | ||
| 40 | + Future<Integer> future = executor.submit(() -> { | ||
| 41 | + try { | ||
| 42 | + TimeUnit.SECONDS.sleep(1); | ||
| 43 | + return 123; | ||
| 44 | + } | ||
| 45 | + catch (InterruptedException e) { | ||
| 46 | + throw new IllegalStateException("task interrupted", e); | ||
| 47 | + } | ||
| 48 | + }); | ||
| 49 | + | ||
| 50 | + executor.shutdownNow(); | ||
| 51 | + future.get(); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + private static void test1() throws InterruptedException, ExecutionException { | ||
| 55 | + ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
| 56 | + | ||
| 57 | + Future<Integer> future = executor.submit(() -> { | ||
| 58 | + try { | ||
| 59 | + TimeUnit.SECONDS.sleep(1); | ||
| 60 | + return 123; | ||
| 61 | + } | ||
| 62 | + catch (InterruptedException e) { | ||
| 63 | + throw new IllegalStateException("task interrupted", e); | ||
| 64 | + } | ||
| 65 | + }); | ||
| 66 | + | ||
| 67 | + System.out.println("future done: " + future.isDone()); | ||
| 68 | + | ||
| 69 | + Integer result = future.get(); | ||
| 70 | + | ||
| 71 | + System.out.println("future done: " + future.isDone()); | ||
| 72 | + System.out.print("result: " + result); | ||
| 73 | + | ||
| 74 | + executor.shutdownNow(); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments