Skip to content

Commit 1f9c9a5

Browse files
committed
..
1 parent 5e23fbc commit 1f9c9a5

18 files changed

+83
-66
lines changed

src/main/java/com/winterbe/java8/samples/concurrent/AtomicIntegerDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static void testAccumulate() {
3535
atomicInt.set(0);
3636
ExecutorService executor = Executors.newFixedThreadPool(2);
3737
IntStream.range(0, NUM_INCREMENTS).forEach(i -> {
38-
Runnable task = () -> atomicInt.accumulateAndGet(i, (n, m) -> n + m);
38+
Runnable task = () -> atomicInt.accumulateAndGet(i, (n, m) -> n + m);
3939
executor.submit(task);
4040
});
4141
ConcurrentUtils.stop(executor);

src/main/java/com/winterbe/java8/samples/concurrent/CompletableFuture1.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
1212
CompletableFuture<String> future = new CompletableFuture<>();
1313
future.complete("42");
1414
future.thenAccept(System.out::println).thenAccept(v -> System.out.println("done"));
15+
1516
}
1617
}

src/main/java/com/winterbe/java8/samples/concurrent/ConcurrentHashMapDemo1.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ private static void testReduce() {
2323
map.putIfAbsent("r2", "d2");
2424
map.putIfAbsent("c3", "p0");
2525

26-
String reduced = map.reduce(1, (key, value) -> key + "=" + value,
27-
(s1, s2) -> s1 + ", " + s2);
26+
String reduced = map.reduce(1, (key, value) -> key + "=" + value, (s1, s2) -> s1 + ", " + s2);
2827

2928
System.out.println(reduced);
3029
}
@@ -68,8 +67,10 @@ private static void testForEach() {
6867
map.putIfAbsent("r2", "d2");
6968
map.putIfAbsent("c3", "p0");
7069

71-
map.forEach(1, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
72-
// map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
70+
map.forEach(1,
71+
(key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
72+
// map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key,
73+
// value, Thread.currentThread().getName()));
7374

7475
System.out.println(map.mappingCount());
7576
}

src/main/java/com/winterbe/java8/samples/concurrent/Executors1.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/executor/Executors1SingleThread.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.winterbe.java8.samples.concurrent;
1+
package com.winterbe.java8.samples.concurrent.executor;
22

33
import java.util.concurrent.ExecutorService;
44
import java.util.concurrent.Executors;
@@ -7,10 +7,10 @@
77
/**
88
* @author Benjamin Winterberg
99
*/
10-
public class Executors1 {
10+
public class Executors1SingleThread {
1111

1212
public static void main(String[] args) {
13-
test1(3);
13+
test1(2);
1414
// test1(7);
1515
}
1616

src/main/java/com/winterbe/java8/samples/concurrent/Executors2.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/executor/Executors2FixedThread.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.winterbe.java8.samples.concurrent;
1+
package com.winterbe.java8.samples.concurrent.executor;
22

33
import java.util.concurrent.ExecutionException;
44
import java.util.concurrent.ExecutorService;
@@ -10,11 +10,12 @@
1010
/**
1111
* @author Benjamin Winterberg
1212
*/
13-
public class Executors2 {
13+
@SuppressWarnings("unused")
14+
public class Executors2FixedThread {
1415

1516
public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
16-
// test1();
17-
// test2();
17+
test1();
18+
// test2();
1819
test3();
1920
}
2021

@@ -23,17 +24,17 @@ private static void test3() throws InterruptedException, ExecutionException, Tim
2324

2425
Future<Integer> future = executor.submit(() -> {
2526
try {
26-
TimeUnit.SECONDS.sleep(2);
27+
TimeUnit.SECONDS.sleep(1);
2728
return 123;
2829
} catch (InterruptedException e) {
2930
throw new IllegalStateException("task interrupted", e);
3031
}
3132
});
3233

33-
future.get(1, TimeUnit.SECONDS);
34+
Integer integer = future.get(2, TimeUnit.SECONDS);
35+
System.out.println("test 3 result : " + integer);
3436
}
3537

36-
@SuppressWarnings("unused")
3738
private static void test2() throws InterruptedException, ExecutionException {
3839
ExecutorService executor = Executors.newFixedThreadPool(1);
3940

@@ -47,10 +48,10 @@ private static void test2() throws InterruptedException, ExecutionException {
4748
});
4849

4950
executor.shutdownNow();
50-
future.get();
51+
Integer integer = future.get();
52+
System.out.println("test2 result : " + integer);
5153
}
5254

53-
@SuppressWarnings("unused")
5455
private static void test1() throws InterruptedException, ExecutionException {
5556
ExecutorService executor = Executors.newFixedThreadPool(1);
5657

@@ -64,11 +65,9 @@ private static void test1() throws InterruptedException, ExecutionException {
6465
});
6566

6667
System.out.println("future done: " + future.isDone());
67-
6868
Integer result = future.get();
69-
7069
System.out.println("future done: " + future.isDone());
71-
System.out.print("result: " + result);
70+
System.out.println("test1 result: " + result);
7271

7372
executor.shutdownNow();
7473
}

src/main/java/com/winterbe/java8/samples/concurrent/Executors3.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/executor/Executors3ScheduledStealingThread.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.winterbe.java8.samples.concurrent;
1+
package com.winterbe.java8.samples.concurrent.executor;
22

33
import java.util.Arrays;
44
import java.util.List;
@@ -13,22 +13,22 @@
1313
/**
1414
* @author Benjamin Winterberg
1515
*/
16-
public class Executors3 {
16+
public class Executors3ScheduledStealingThread {
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
@SuppressWarnings("unused")
2828
private static void test5() throws InterruptedException, ExecutionException {
2929
ExecutorService executor = Executors.newWorkStealingPool();
3030

31-
List<Callable<String>> callables = Arrays.asList(callable("task1", 2), callable("task2", 1), callable("task3", 3));
31+
List<Callable<String>> callables = Arrays.asList(callable("task1", 2), callable("task2", 4), callable("task3", 3));
3232

3333
String result = executor.invokeAny(callables);
3434
System.out.println(result);

src/main/java/com/winterbe/java8/samples/concurrent/Lock1.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/lock/Lock1.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
package com.winterbe.java8.samples.concurrent;
1+
package com.winterbe.java8.samples.concurrent.lock;
22

33
import java.util.concurrent.ExecutorService;
44
import java.util.concurrent.Executors;
55
import java.util.concurrent.locks.ReentrantLock;
66
import java.util.stream.IntStream;
77

8+
import com.winterbe.java8.samples.concurrent.ConcurrentUtils;
9+
810
/**
911
* @author Benjamin Winterberg
1012
*/
1113
public class Lock1 {
1214

1315
private static final int NUM_INCREMENTS = 10000;
14-
1516
private static ReentrantLock lock = new ReentrantLock();
16-
1717
private static int count = 0;
1818

19+
public static void main(String[] args) {
20+
testLock();
21+
}
22+
1923
private static void increment() {
2024
lock.lock();
2125
try {
@@ -25,19 +29,11 @@ private static void increment() {
2529
}
2630
}
2731

28-
public static void main(String[] args) {
29-
testLock();
30-
}
31-
3232
private static void testLock() {
3333
count = 0;
34-
3534
ExecutorService executor = Executors.newFixedThreadPool(2);
36-
3735
IntStream.range(0, NUM_INCREMENTS).forEach(i -> executor.submit(Lock1::increment));
38-
3936
ConcurrentUtils.stop(executor);
40-
4137
System.out.println(count);
4238
}
4339

src/main/java/com/winterbe/java8/samples/concurrent/Lock2.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/lock/Lock2.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package com.winterbe.java8.samples.concurrent;
1+
package com.winterbe.java8.samples.concurrent.lock;
22

33
import java.util.concurrent.ExecutorService;
44
import java.util.concurrent.Executors;
55
import java.util.concurrent.locks.ReentrantLock;
66

7+
import com.winterbe.java8.samples.concurrent.ConcurrentUtils;
8+
79
/**
810
* @author Benjamin Winterberg
911
*/

src/main/java/com/winterbe/java8/samples/concurrent/Lock3.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/lock/Lock3.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.winterbe.java8.samples.concurrent;
1+
package com.winterbe.java8.samples.concurrent.lock;
22

33
import java.util.HashMap;
44
import java.util.Map;
@@ -7,6 +7,8 @@
77
import java.util.concurrent.locks.ReadWriteLock;
88
import java.util.concurrent.locks.ReentrantReadWriteLock;
99

10+
import com.winterbe.java8.samples.concurrent.ConcurrentUtils;
11+
1012
/**
1113
* @author Benjamin Winterberg
1214
*/

src/main/java/com/winterbe/java8/samples/concurrent/Lock4.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/lock/Lock4.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
package com.winterbe.java8.samples.concurrent;
1+
package com.winterbe.java8.samples.concurrent.lock;
22

33
import java.util.HashMap;
44
import java.util.Map;
55
import java.util.concurrent.ExecutorService;
66
import java.util.concurrent.Executors;
77
import java.util.concurrent.locks.StampedLock;
88

9+
import com.winterbe.java8.samples.concurrent.ConcurrentUtils;
10+
911
/**
1012
* @author Benjamin Winterberg
1113
*/
1214
public class Lock4 {
1315

1416
public static void main(String[] args) {
1517
ExecutorService executor = Executors.newFixedThreadPool(2);
16-
1718
Map<String, String> map = new HashMap<>();
18-
1919
StampedLock lock = new StampedLock();
20-
2120
executor.submit(() -> {
2221
long stamp = lock.writeLock();
2322
try {
@@ -31,7 +30,7 @@ public static void main(String[] args) {
3130
Runnable readTask = () -> {
3231
long stamp = lock.readLock();
3332
try {
34-
System.out.println(map.get("foo"));
33+
System.out.println("read: "+map.get("foo"));
3534
ConcurrentUtils.sleep(1);
3635
} finally {
3736
lock.unlockRead(stamp);

0 commit comments

Comments
 (0)