| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 043b25d commit efd7a79
34 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,26 +3,27 @@ | |||
| 3 | 3 | // We make no guarantees that this code is fit for any purpose. | |
| 4 | 4 | // Visit http://OnJava8.com for more book information. | |
| 5 | 5 | import java.util.concurrent.*; | |
| 6 | + import java.util.stream.*; | ||
| 6 | 7 | ||
| 7 | 8 | public class CachedThreadPool { | |
| 8 | - public static void main(String[] args) | ||
| 9 | - throws InterruptedException { | ||
| 9 | + public static void main(String[] args) { | ||
| 10 | 10 | ExecutorService exec = | |
| 11 | 11 | Executors.newCachedThreadPool(); | |
| 12 | - for(int id = 0; id < 10; id++) | ||
| 13 | - exec.execute(new SleepAndPrintTask(id)); | ||
| 12 | + IntStream.range(0, 10) | ||
| 13 | + .mapToObj(NapTask::new) | ||
| 14 | + .forEach(exec::execute); | ||
| 14 | 15 | exec.shutdown(); | |
| 15 | 16 | } | |
| 16 | 17 | } | |
| 17 | 18 | /* Output: | |
| 18 | - SleepAndPrintTask[2] pool-1-thread-3 | ||
| 19 | - SleepAndPrintTask[9] pool-1-thread-10 | ||
| 20 | - SleepAndPrintTask[6] pool-1-thread-7 | ||
| 21 | - SleepAndPrintTask[5] pool-1-thread-6 | ||
| 22 | - SleepAndPrintTask[7] pool-1-thread-8 | ||
| 23 | - SleepAndPrintTask[8] pool-1-thread-9 | ||
| 24 | - SleepAndPrintTask[0] pool-1-thread-1 | ||
| 25 | - SleepAndPrintTask[1] pool-1-thread-2 | ||
| 26 | - SleepAndPrintTask[4] pool-1-thread-5 | ||
| 27 | - SleepAndPrintTask[3] pool-1-thread-4 | ||
| 19 | + NapTask[2] pool-1-thread-3 | ||
| 20 | + NapTask[9] pool-1-thread-10 | ||
| 21 | + NapTask[6] pool-1-thread-7 | ||
| 22 | + NapTask[5] pool-1-thread-6 | ||
| 23 | + NapTask[7] pool-1-thread-8 | ||
| 24 | + NapTask[8] pool-1-thread-9 | ||
| 25 | + NapTask[0] pool-1-thread-1 | ||
| 26 | + NapTask[1] pool-1-thread-2 | ||
| 27 | + NapTask[4] pool-1-thread-5 | ||
| 28 | + NapTask[3] pool-1-thread-4 | ||
| 28 | 29 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,14 +3,15 @@ | |||
| 3 | 3 | // We make no guarantees that this code is fit for any purpose. | |
| 4 | 4 | // Visit http://OnJava8.com for more book information. | |
| 5 | 5 | import java.util.concurrent.*; | |
| 6 | + import java.util.stream.*; | ||
| 6 | 7 | ||
| 7 | 8 | public class CachedThreadPool2 { | |
| 8 | - public static void main(String[] args) | ||
| 9 | - throws InterruptedException { | ||
| 9 | + public static void main(String[] args) { | ||
| 10 | 10 | ExecutorService exec = | |
| 11 | 11 | Executors.newCachedThreadPool(); | |
| 12 | - for(int id = 0; id < 10; id++) | ||
| 13 | - exec.execute(new InterferingTask(id)); | ||
| 12 | + IntStream.range(0, 10) | ||
| 13 | + .mapToObj(InterferingTask::new) | ||
| 14 | + .forEach(exec::execute); | ||
| 14 | 15 | exec.shutdown(); | |
| 15 | 16 | } | |
| 16 | 17 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,21 @@ | |||
| 1 | + // concurrent/CompletableApply.java | ||
| 2 | + // (c)2016 MindView LLC: see Copyright.txt | ||
| 3 | + // We make no guarantees that this code is fit for any purpose. | ||
| 4 | + // Visit http://OnJava8.com for more book information. | ||
| 5 | + import java.util.concurrent.*; | ||
| 6 | + | ||
| 7 | + public class CompletableApply { | ||
| 8 | + public static void main(String[] args) { | ||
| 9 | + CompletableFuture<Machina> cf = | ||
| 10 | + CompletableFuture.completedFuture( | ||
| 11 | + new Machina(0)); | ||
| 12 | + CompletableFuture<Machina> cf2 = | ||
| 13 | + cf.thenApply(Machina::work); | ||
| 14 | + CompletableFuture<Machina> cf3 = | ||
| 15 | + cf2.thenApply(Machina::work); | ||
| 16 | + CompletableFuture<Machina> cf4 = | ||
| 17 | + cf3.thenApply(Machina::work); | ||
| 18 | + CompletableFuture<Machina> cf5 = | ||
| 19 | + cf4.thenApply(Machina::work); | ||
| 20 | + } | ||
| 21 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,31 @@ | |||
| 1 | + // concurrent/CompletableApplyAsync.java | ||
| 2 | + // (c)2016 MindView LLC: see Copyright.txt | ||
| 3 | + // We make no guarantees that this code is fit for any purpose. | ||
| 4 | + // Visit http://OnJava8.com for more book information. | ||
| 5 | + import java.util.concurrent.*; | ||
| 6 | + import onjava.*; | ||
| 7 | + | ||
| 8 | + public class CompletableApplyAsync { | ||
| 9 | + public static void main(String[] args) { | ||
| 10 | + Timer timer = new Timer(); | ||
| 11 | + CompletableFuture<Machina> cf = | ||
| 12 | + CompletableFuture.completedFuture( | ||
| 13 | + new Machina(0)) | ||
| 14 | + .thenApplyAsync(Machina::work) | ||
| 15 | + .thenApplyAsync(Machina::work) | ||
| 16 | + .thenApplyAsync(Machina::work) | ||
| 17 | + .thenApplyAsync(Machina::work); | ||
| 18 | + System.out.println(timer.duration()); | ||
| 19 | + System.out.println(cf.join()); | ||
| 20 | + System.out.println(timer.duration()); | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + /* Output: | ||
| 24 | + 50 | ||
| 25 | + Machina0: ONE | ||
| 26 | + Machina0: TWO | ||
| 27 | + Machina0: THREE | ||
| 28 | + Machina0: complete | ||
| 29 | + Machina0: complete | ||
| 30 | + 462 | ||
| 31 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + // concurrent/CompletableApplyChained.java | ||
| 2 | + // (c)2016 MindView LLC: see Copyright.txt | ||
| 3 | + // We make no guarantees that this code is fit for any purpose. | ||
| 4 | + // Visit http://OnJava8.com for more book information. | ||
| 5 | + import java.util.concurrent.*; | ||
| 6 | + import onjava.Timer; | ||
| 7 | + | ||
| 8 | + public class CompletableApplyChained { | ||
| 9 | + public static void main(String[] args) { | ||
| 10 | + Timer timer = new Timer(); | ||
| 11 | + CompletableFuture<Machina> cf = | ||
| 12 | + CompletableFuture.completedFuture( | ||
| 13 | + new Machina(0)) | ||
| 14 | + .thenApply(Machina::work) | ||
| 15 | + .thenApply(Machina::work) | ||
| 16 | + .thenApply(Machina::work) | ||
| 17 | + .thenApply(Machina::work); | ||
| 18 | + System.out.println(timer.duration()); | ||
| 19 | + } | ||
| 20 | + } | ||
| 21 | + /* Output: | ||
| 22 | + Machina0: ONE | ||
| 23 | + Machina0: TWO | ||
| 24 | + Machina0: THREE | ||
| 25 | + Machina0: complete | ||
| 26 | + 460 | ||
| 27 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + // concurrent/CompletableOperations.java | ||
| 2 | + // (c)2016 MindView LLC: see Copyright.txt | ||
| 3 | + // We make no guarantees that this code is fit for any purpose. | ||
| 4 | + // Visit http://OnJava8.com for more book information. | ||
| 5 | + import java.util.concurrent.*; | ||
| 6 | + | ||
| 7 | + public class CompletableOperations { | ||
| 8 | + static CompletableFuture<Integer> cfi() { | ||
| 9 | + return | ||
| 10 | + CompletableFuture.completedFuture( | ||
| 11 | + new Integer(1)); | ||
| 12 | + } | ||
| 13 | + // Get and show value stored in a CF: | ||
| 14 | + static void showr(CompletableFuture<Integer> c) { | ||
| 15 | + try { | ||
| 16 | + System.out.println(c.get()); | ||
| 17 | + } catch(InterruptedException | ||
| 18 | + | ExecutionException e) { | ||
| 19 | + throw new RuntimeException(e); | ||
| 20 | + } | ||
| 21 | + } | ||
| 22 | + // For CF operations that have no value: | ||
| 23 | + static void voidr(CompletableFuture<Void> c) { | ||
| 24 | + try { | ||
| 25 | + c.get(); // Returns void | ||
| 26 | + } catch(InterruptedException | ||
| 27 | + | ExecutionException e) { | ||
| 28 | + throw new RuntimeException(e); | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + public static void main(String[] args) { | ||
| 32 | + showr(cfi()); // Basic test | ||
| 33 | + showr(cfi().thenApplyAsync(i -> i + 42)); | ||
| 34 | + voidr(cfi().runAsync(() -> System.out.println("run"))); | ||
| 35 | + CompletableFuture<Integer> c = cfi(); | ||
| 36 | + c.obtrudeValue(111); | ||
| 37 | + showr(c); | ||
| 38 | + } | ||
| 39 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,45 @@ | |||
| 1 | + // concurrent/CompletablePizza.java | ||
| 2 | + // (c)2016 MindView LLC: see Copyright.txt | ||
| 3 | + // We make no guarantees that this code is fit for any purpose. | ||
| 4 | + // Visit http://OnJava8.com for more book information. | ||
| 5 | + import java.util.*; | ||
| 6 | + import java.util.concurrent.*; | ||
| 7 | + import java.util.stream.*; | ||
| 8 | + import onjava.Timer; | ||
| 9 | + | ||
| 10 | + public class CompletablePizza { | ||
| 11 | + static int QUANTITY = 5; | ||
| 12 | + public static CompletableFuture<Pizza> | ||
| 13 | + makeCF(Pizza za) { | ||
| 14 | + return CompletableFuture | ||
| 15 | + .completedFuture(za) | ||
| 16 | + .thenApplyAsync(Pizza::roll) | ||
| 17 | + .thenApplyAsync(Pizza::sauce) | ||
| 18 | + .thenApplyAsync(Pizza::cheese) | ||
| 19 | + .thenApplyAsync(Pizza::toppings) | ||
| 20 | + .thenApplyAsync(Pizza::bake) | ||
| 21 | + .thenApplyAsync(Pizza::slice) | ||
| 22 | + .thenApplyAsync(Pizza::box); | ||
| 23 | + } | ||
| 24 | + public static void | ||
| 25 | + show(CompletableFuture<Pizza> cf) { | ||
| 26 | + try { | ||
| 27 | + System.out.println(cf.get()); | ||
| 28 | + } catch(Exception e) { | ||
| 29 | + throw new RuntimeException(e); | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + public static void main(String[] args) { | ||
| 33 | + Timer timer = new Timer(); | ||
| 34 | + List<CompletableFuture<Pizza>> pizzas = | ||
| 35 | + IntStream.range(0, QUANTITY) | ||
| 36 | + .mapToObj(Pizza::new) | ||
| 37 | + .map(CompletablePizza::makeCF) | ||
| 38 | + .collect(Collectors.toList()); | ||
| 39 | + System.out.println(timer.duration()); | ||
| 40 | + pizzas.forEach(CompletablePizza::show); | ||
| 41 | + System.out.println(timer.duration()); | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | + /* Output: | ||
| 45 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + // concurrent/CompletedMachina.java | ||
| 2 | + // (c)2016 MindView LLC: see Copyright.txt | ||
| 3 | + // We make no guarantees that this code is fit for any purpose. | ||
| 4 | + // Visit http://OnJava8.com for more book information. | ||
| 5 | + import java.util.concurrent.*; | ||
| 6 | + | ||
| 7 | + public class CompletedMachina { | ||
| 8 | + public static void main(String[] args) { | ||
| 9 | + CompletableFuture<Machina> cf = | ||
| 10 | + CompletableFuture.completedFuture( | ||
| 11 | + new Machina(0)); | ||
| 12 | + try { | ||
| 13 | + Machina m = cf.get(); // Doesn't block | ||
| 14 | + } catch(InterruptedException | | ||
| 15 | + ExecutionException e) { | ||
| 16 | + throw new RuntimeException(e); | ||
| 17 | + } | ||
| 18 | + } | ||
| 19 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,10 +5,10 @@ | |||
| 5 | 5 | // Demonstrates how deadlock can be hidden in a program | |
| 6 | 6 | // {java DeadlockingDiningPhilosophers 0 5 timeout} | |
| 7 | 7 | import java.util.concurrent.*; | |
| 8 | + import onjava.Nap; | ||
| 8 | 9 | ||
| 9 | 10 | public class DeadlockingDiningPhilosophers { | |
| 10 | - public static void | ||
| 11 | - main(String[] args) throws Exception { | ||
| 11 | + public static void main(String[] args) { | ||
| 12 | 12 | int ponder = 5; | |
| 13 | 13 | if(args.length > 0) | |
| 14 | 14 | ponder = Integer.parseInt(args[0]); | |
@@ -23,10 +23,14 @@ public class DeadlockingDiningPhilosophers { | |||
| 23 | 23 | es.execute(new Philosopher( | |
| 24 | 24 | sticks[i], sticks[(i+1) % size], i, ponder)); | |
| 25 | 25 | if(args.length == 3 && args[2].equals("timeout")) | |
| 26 | - TimeUnit.SECONDS.sleep(5); | ||
| 26 | + new Nap(5000); | ||
| 27 | 27 | else { | |
| 28 | 28 | System.out.println("Press 'Enter' to quit"); | |
| 29 | - System.in.read(); | ||
| 29 | + try { | ||
| 30 | + System.in.read(); | ||
| 31 | + } catch(Exception e) { | ||
| 32 | + throw new RuntimeException(e); | ||
| 33 | + } | ||
| 30 | 34 | } | |
| 31 | 35 | es.shutdownNow(); | |
| 32 | 36 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,46 @@ | |||
| 1 | + // concurrent/DualCompletableOperations.java | ||
| 2 | + // (c)2016 MindView LLC: see Copyright.txt | ||
| 3 | + // We make no guarantees that this code is fit for any purpose. | ||
| 4 | + // Visit http://OnJava8.com for more book information. | ||
| 5 | + import java.util.concurrent.*; | ||
| 6 | + | ||
| 7 | + public class DualCompletableOperations { | ||
| 8 | + static | ||
| 9 | + CompletableFuture<RandomWork> makeCF(int id) { | ||
| 10 | + return | ||
| 11 | + CompletableFuture.completedFuture( | ||
| 12 | + new RandomWork(id)) | ||
| 13 | + .thenApplyAsync(RandomWork::work); | ||
| 14 | + } | ||
| 15 | + static CompletableFuture<RandomWork> cfA, cfB; | ||
| 16 | + static int count = 0; | ||
| 17 | + static void init() { | ||
| 18 | + cfA = makeCF(count++); | ||
| 19 | + cfB = makeCF(count++); | ||
| 20 | + } | ||
| 21 | + static void join() { | ||
| 22 | + cfA.join(); | ||
| 23 | + cfB.join(); | ||
| 24 | + } | ||
| 25 | + public static void main(String[] args) { | ||
| 26 | + init(); | ||
| 27 | + cfA.runAfterBothAsync(cfB, () -> | ||
| 28 | + System.out.println("Completed Both")); | ||
| 29 | + join(); | ||
| 30 | + | ||
| 31 | + init(); | ||
| 32 | + cfA.runAfterEitherAsync(cfB, () -> | ||
| 33 | + System.out.println("Completed Either")); | ||
| 34 | + join(); | ||
| 35 | + | ||
| 36 | + init(); | ||
| 37 | + cfA.applyToEitherAsync(cfB, rw -> { | ||
| 38 | + System.out.println("applyToEither: " + rw); | ||
| 39 | + return rw; | ||
| 40 | + }); | ||
| 41 | + join(); | ||
| 42 | + | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + /* Output: | ||
| 46 | + */ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments