Skip to content

Commit 043b25d

Browse files
committed
Timer.java, bump Gradle version, remove awaitTermination, Pizza
1 parent e9cb665 commit 043b25d

20 files changed

+232
-52
lines changed

concurrent/CachedThreadPool.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public static void main(String[] args)
1212
for(int id = 0; id < 10; id++)
1313
exec.execute(new SleepAndPrintTask(id));
1414
exec.shutdown();
15-
exec.awaitTermination(5, TimeUnit.SECONDS);
1615
}
1716
}
1817
/* Output:

concurrent/CachedThreadPool2.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public static void main(String[] args)
1212
for(int id = 0; id < 10; id++)
1313
exec.execute(new InterferingTask(id));
1414
exec.shutdown();
15-
exec.awaitTermination(5, TimeUnit.SECONDS);
1615
}
1716
}
1817
/* Output:

concurrent/LambdasAndMethodReferences.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static void main(String[] args)
3030
});
3131
exec.submit(new NotCallable()::get);
3232
exec.shutdown();
33-
exec.awaitTermination(1, TimeUnit.SECONDS);
3433
}
3534
}
3635
/* Output:

concurrent/MoreTasksAfterShutdown.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static void main(String[] args)
1616
} catch(RejectedExecutionException e) {
1717
System.out.println(e);
1818
}
19-
exec.awaitTermination(5, TimeUnit.SECONDS);
2019
}
2120
}
2221
/* Output:

concurrent/OnePizza.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// concurrent/OnePizza.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 onjava.Timer;
6+
7+
public class OnePizza {
8+
public static void main(String[] args) {
9+
Pizza za = new Pizza(0);
10+
System.out.println(
11+
Timer.duration(() -> {
12+
while(!za.complete())
13+
za.next();
14+
}));
15+
}
16+
}
17+
/* Output:
18+
Pizza 0: ROLLED
19+
Pizza 0: SAUCED
20+
Pizza 0: CHEESED
21+
Pizza 0: TOPPED
22+
Pizza 0: BAKED
23+
Pizza 0: SLICED
24+
Pizza 0: BOXED
25+
1612
26+
*/

concurrent/ParallelPrime.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.*;
99
import java.nio.file.*;
1010
import java.nio.charset.*;
11+
import onjava.Timer;
1112

1213
public class ParallelPrime {
1314
static final int COUNT = 100_000;
@@ -17,16 +18,15 @@ public static boolean isPrime(long n) {
1718
}
1819
public static void main(String[] args)
1920
throws IOException {
20-
long start = System.currentTimeMillis();
21+
Timer timer = new Timer();
2122
List<String> primes =
2223
iterate(2, i -> i + 1)
2324
.parallel() // [1]
2425
.filter(ParallelPrime::isPrime)
2526
.limit(COUNT)
2627
.mapToObj(Long::toString)
2728
.collect(Collectors.toList());
28-
System.out.println(
29-
System.currentTimeMillis() - start);
29+
System.out.println(timer.duration());
3030
Files.write(Paths.get("primes.txt"), primes,
3131
StandardOpenOption.CREATE);
3232
}

concurrent/Pizza.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// concurrent/Pizza.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 static java.util.concurrent.TimeUnit.*;
6+
7+
public class Pizza {
8+
public enum Step {
9+
DOUGH(4), ROLLED(1), SAUCED(1), CHEESED(2),
10+
TOPPED(5), BAKED(2), SLICED(1), BOXED(0);
11+
int effort; // Needed to get to the next step
12+
Step(int effort) { this.effort = effort; }
13+
Step forward() {
14+
if(equals(BOXED)) return BOXED;
15+
try {
16+
MILLISECONDS.sleep(effort * 100);
17+
} catch(InterruptedException e) {
18+
throw new RuntimeException(e);
19+
}
20+
return values()[ordinal() + 1];
21+
}
22+
}
23+
private Step step = Step.DOUGH;
24+
private final int id;
25+
public Pizza(int id) { this.id = id; }
26+
public void next() {
27+
step = step.forward();
28+
System.out.println("Pizza " + id + ": " + step);
29+
}
30+
public void next(Step previousStep) {
31+
if(!step.equals(previousStep))
32+
throw new IllegalStateException("Expected " +
33+
previousStep + " but found " + step);
34+
next();
35+
}
36+
public void roll() { next(Step.DOUGH); }
37+
public void sauce() { next(Step.ROLLED); }
38+
public void cheese() { next(Step.SAUCED); }
39+
public void toppings() { next(Step.CHEESED); }
40+
public void bake() { next(Step.TOPPED); }
41+
public void slice() { next(Step.BAKED); }
42+
public void box() { next(Step.SLICED); }
43+
public boolean complete() {
44+
return step.equals(Step.BOXED);
45+
}
46+
}

concurrent/PizzaStreams.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// concurrent/PizzaStreams.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.stream.*;
7+
import onjava.Timer;
8+
9+
public class PizzaStreams {
10+
static int QUANTITY = 5;
11+
public static void main(String[] args) {
12+
Timer timer = new Timer();
13+
IntStream.range(0, QUANTITY)
14+
.mapToObj(Pizza::new)
15+
.parallel() // [1]
16+
.forEach(za -> {
17+
while(!za.complete())
18+
za.next();
19+
});
20+
System.out.println(timer.duration());
21+
}
22+
}
23+
/* Output:
24+
Pizza 4: ROLLED
25+
Pizza 2: ROLLED
26+
Pizza 0: ROLLED
27+
Pizza 3: ROLLED
28+
Pizza 1: ROLLED
29+
Pizza 0: SAUCED
30+
Pizza 3: SAUCED
31+
Pizza 4: SAUCED
32+
Pizza 1: SAUCED
33+
Pizza 2: SAUCED
34+
Pizza 0: CHEESED
35+
Pizza 4: CHEESED
36+
Pizza 3: CHEESED
37+
Pizza 2: CHEESED
38+
Pizza 1: CHEESED
39+
Pizza 3: TOPPED
40+
Pizza 4: TOPPED
41+
Pizza 0: TOPPED
42+
Pizza 2: TOPPED
43+
Pizza 1: TOPPED
44+
Pizza 0: BAKED
45+
Pizza 3: BAKED
46+
Pizza 4: BAKED
47+
Pizza 1: BAKED
48+
Pizza 2: BAKED
49+
Pizza 4: SLICED
50+
Pizza 3: SLICED
51+
Pizza 0: SLICED
52+
Pizza 2: SLICED
53+
Pizza 1: SLICED
54+
Pizza 3: BOXED
55+
Pizza 4: BOXED
56+
Pizza 0: BOXED
57+
Pizza 1: BOXED
58+
Pizza 2: BOXED
59+
1667
60+
*/

concurrent/QuittableTask.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// concurrent/QuittableTask.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 java.util.concurrent.atomic.AtomicBoolean;
7+
8+
public class QuittableTask implements Runnable {
9+
final int id;
10+
public QuittableTask(int id) { this.id = id; }
11+
private AtomicBoolean running =
12+
new AtomicBoolean(true);
13+
public void quit() { running.set(false); }
14+
@Override
15+
public void run() {
16+
while(running.get()) // [1]
17+
try {
18+
TimeUnit.MILLISECONDS.sleep(100);
19+
} catch(InterruptedException e) {
20+
throw new RuntimeException(e);
21+
}
22+
System.out.print(id + " "); // [2]
23+
}
24+
}

concurrent/QuittingTasks.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// concurrent/QuittingTasks.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.stream.*;
7+
import java.util.concurrent.*;
8+
9+
public class QuittingTasks {
10+
public static final int COUNT = 150;
11+
public static void main(String[] args)
12+
throws InterruptedException {
13+
ExecutorService es =
14+
Executors.newCachedThreadPool();
15+
List<QuittableTask> tasks =
16+
IntStream.range(1, COUNT)
17+
.mapToObj(QuittableTask::new)
18+
.peek(qt -> es.execute(qt))
19+
.collect(Collectors.toList());
20+
TimeUnit.SECONDS.sleep(1);
21+
tasks.forEach(QuittableTask::quit);
22+
es.shutdown();
23+
}
24+
}

0 commit comments

Comments
 (0)