Skip to content

Commit efd7a79

Browse files
committed
Concurrency chapter progress
1 parent 043b25d commit efd7a79

34 files changed

+493
-165
lines changed

concurrent/CachedThreadPool.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import java.util.concurrent.*;
6+
import java.util.stream.*;
67

78
public class CachedThreadPool {
8-
public static void main(String[] args)
9-
throws InterruptedException {
9+
public static void main(String[] args) {
1010
ExecutorService exec =
1111
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);
1415
exec.shutdown();
1516
}
1617
}
1718
/* 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
2829
*/

concurrent/CachedThreadPool2.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import java.util.concurrent.*;
6+
import java.util.stream.*;
67

78
public class CachedThreadPool2 {
8-
public static void main(String[] args)
9-
throws InterruptedException {
9+
public static void main(String[] args) {
1010
ExecutorService exec =
1111
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);
1415
exec.shutdown();
1516
}
1617
}

concurrent/CompletableApply.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

concurrent/CompletablePizza.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
*/

concurrent/CompletedMachina.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

concurrent/DeadlockingDiningPhilosophers.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
// Demonstrates how deadlock can be hidden in a program
66
// {java DeadlockingDiningPhilosophers 0 5 timeout}
77
import java.util.concurrent.*;
8+
import onjava.Nap;
89

910
public class DeadlockingDiningPhilosophers {
10-
public static void
11-
main(String[] args) throws Exception {
11+
public static void main(String[] args) {
1212
int ponder = 5;
1313
if(args.length > 0)
1414
ponder = Integer.parseInt(args[0]);
@@ -23,10 +23,14 @@ public class DeadlockingDiningPhilosophers {
2323
es.execute(new Philosopher(
2424
sticks[i], sticks[(i+1) % size], i, ponder));
2525
if(args.length == 3 && args[2].equals("timeout"))
26-
TimeUnit.SECONDS.sleep(5);
26+
new Nap(5000);
2727
else {
2828
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+
}
3034
}
3135
es.shutdownNow();
3236
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
*/

0 commit comments

Comments
 (0)