Skip to content

Commit 89a2571

Browse files
committed
Executors example
1 parent bd2e991 commit 89a2571

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

src/com/winterbe/java8/samples/concurrent/Executors1.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ private static void test1(long seconds) {
1919
executor.submit(() -> {
2020
try {
2121
TimeUnit.SECONDS.sleep(seconds);
22-
System.out.println("task finished: " + Thread.currentThread().getName());
22+
String name = Thread.currentThread().getName();
23+
System.out.println("task finished: " + name);
2324
}
2425
catch (InterruptedException e) {
2526
System.err.println("task interrupted");
2627
}
2728
});
28-
shutdown(executor);
29+
stop(executor);
2930
}
3031

31-
static void shutdown(ExecutorService executor) {
32+
static void stop(ExecutorService executor) {
3233
try {
3334
System.out.println("attempt to shutdown executor");
3435
executor.shutdown();
@@ -38,6 +39,9 @@ static void shutdown(ExecutorService executor) {
3839
System.err.println("termination interrupted");
3940
}
4041
finally {
42+
if (!executor.isTerminated()) {
43+
System.err.println("killing non-finished tasks");
44+
}
4145
executor.shutdownNow();
4246
System.out.println("shutdown finished");
4347
}

src/com/winterbe/java8/samples/concurrent/Executors2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
1919
}
2020

2121
private static void test3() throws InterruptedException, ExecutionException, TimeoutException {
22-
ExecutorService executor = Executors.newSingleThreadExecutor();
22+
ExecutorService executor = Executors.newFixedThreadPool(1);
2323

2424
Future<Integer> future = executor.submit(() -> {
2525
try {
@@ -35,7 +35,7 @@ private static void test3() throws InterruptedException, ExecutionException, Tim
3535
}
3636

3737
private static void test2() throws InterruptedException, ExecutionException {
38-
ExecutorService executor = Executors.newSingleThreadExecutor();
38+
ExecutorService executor = Executors.newFixedThreadPool(1);
3939

4040
Future<Integer> future = executor.submit(() -> {
4141
try {
@@ -52,7 +52,7 @@ private static void test2() throws InterruptedException, ExecutionException {
5252
}
5353

5454
private static void test1() throws InterruptedException, ExecutionException {
55-
ExecutorService executor = Executors.newSingleThreadExecutor();
55+
ExecutorService executor = Executors.newFixedThreadPool(1);
5656

5757
Future<Integer> future = executor.submit(() -> {
5858
try {

src/com/winterbe/java8/samples/concurrent/Executors3.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
public class Executors3 {
1717

1818
public static void main(String[] args) throws InterruptedException, ExecutionException {
19-
// test1();
19+
test1();
2020
// test2();
2121
// test3();
2222

2323
// test4();
24-
test5();
24+
// test5();
2525
}
2626

2727
private static void test5() throws InterruptedException, ExecutionException {
@@ -70,26 +70,37 @@ private static void test4() throws InterruptedException {
7070

7171
private static void test3() {
7272
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
73-
executor.scheduleWithFixedDelay(() -> {
73+
74+
Runnable task = () -> {
7475
try {
7576
TimeUnit.SECONDS.sleep(2);
7677
System.out.println("Scheduling: " + System.nanoTime());
7778
}
7879
catch (InterruptedException e) {
7980
System.err.println("task interrupted");
8081
}
81-
}, 0, 1, TimeUnit.SECONDS);
82+
};
83+
84+
executor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS);
8285
}
8386

8487
private static void test2() {
8588
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
86-
executor.scheduleAtFixedRate(() -> System.out.println("Scheduling: " + System.nanoTime()), 0, 1, TimeUnit.SECONDS);
89+
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
90+
int initialDelay = 0;
91+
int period = 1;
92+
executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
8793
}
8894

8995
private static void test1() throws InterruptedException {
9096
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
91-
ScheduledFuture<?> future = executor.schedule(() -> System.out.println("Scheduling: " + System.nanoTime()), 3, TimeUnit.SECONDS);
97+
98+
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
99+
int delay = 3;
100+
ScheduledFuture<?> future = executor.schedule(task, delay, TimeUnit.SECONDS);
101+
92102
TimeUnit.MILLISECONDS.sleep(1337);
103+
93104
long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS);
94105
System.out.printf("Remaining Delay: %sms\n", remainingDelay);
95106
}

src/com/winterbe/java8/samples/concurrent/Threads1.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public class Threads1 {
99

1010
public static void main(String[] args) {
1111
test1();
12-
test2();
13-
test3();
12+
// test2();
13+
// test3();
1414
}
1515

1616
private static void test3() {
@@ -46,13 +46,16 @@ private static void test2() {
4646
}
4747

4848
private static void test1() {
49-
Runnable runnable = () -> System.out.println("Hello " + Thread.currentThread().getName());
49+
Runnable runnable = () -> {
50+
String threadName = Thread.currentThread().getName();
51+
System.out.println("Hello " + threadName);
52+
};
5053

5154
runnable.run();
5255

5356
Thread thread = new Thread(runnable);
5457
thread.start();
5558

56-
System.out.println("Nachos!");
59+
System.out.println("Done!");
5760
}
5861
}

0 commit comments

Comments
 (0)