Skip to content

Commit aad683e

Browse files
committed
📝 add concurrent code example
1 parent c9cba5b commit aad683e

28 files changed

Lines changed: 935 additions & 368 deletions
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.github.biezhi.java8.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
import java.util.stream.IntStream;
7+
8+
/**
9+
* 原子变量
10+
* <p>
11+
* AtomicInteger
12+
* LongAdder
13+
* LongAccumulator
14+
*
15+
* @author biezhi
16+
* @date 2018/3/5
17+
*/
18+
public class Atomic1 {
19+
20+
private static final int NUM_INCREMENTS = 1000;
21+
22+
private static AtomicInteger atomicInt = new AtomicInteger(0);
23+
24+
public static void main(String[] args) {
25+
testIncrement();
26+
// testAccumulate();
27+
// testUpdate();
28+
}
29+
30+
private static void testUpdate() {
31+
atomicInt.set(0);
32+
33+
ExecutorService executor = Executors.newFixedThreadPool(2);
34+
35+
IntStream.range(0, NUM_INCREMENTS)
36+
.forEach(i -> {
37+
Runnable task = () ->
38+
atomicInt.updateAndGet(n -> n + 2);
39+
executor.submit(task);
40+
});
41+
42+
ConcurrentUtils.stop(executor);
43+
44+
System.out.format("Update: %d\n", atomicInt.get());
45+
}
46+
47+
private static void testAccumulate() {
48+
atomicInt.set(0);
49+
50+
ExecutorService executor = Executors.newFixedThreadPool(2);
51+
52+
IntStream.range(0, NUM_INCREMENTS)
53+
.forEach(i -> {
54+
Runnable task = () ->
55+
atomicInt.accumulateAndGet(i, (n, m) -> n + m);
56+
executor.submit(task);
57+
});
58+
59+
ConcurrentUtils.stop(executor);
60+
61+
System.out.format("Accumulate: %d\n", atomicInt.get());
62+
}
63+
64+
private static void testIncrement() {
65+
atomicInt.set(0);
66+
67+
ExecutorService executor = Executors.newFixedThreadPool(2);
68+
69+
IntStream.range(0, NUM_INCREMENTS)
70+
.forEach(i -> executor.submit(atomicInt::incrementAndGet));
71+
72+
ConcurrentUtils.stop(executor);
73+
74+
System.out.format("Increment: Expected=%d; Is=%d\n", NUM_INCREMENTS, atomicInt.get());
75+
}
76+
}

java8-concurrent/src/main/java/io/github/biezhi/java8/concurrent/AtomicExample.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

java8-concurrent/src/main/java/io/github/biezhi/java8/concurrent/CallableExample.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.github.biezhi.java8.concurrent;
2+
3+
import java.util.concurrent.ConcurrentHashMap;
4+
import java.util.concurrent.ForkJoinPool;
5+
6+
public class ConcurrentHashMap1 {
7+
8+
public static void main(String[] args) {
9+
System.out.println("Parallelism: " + ForkJoinPool.getCommonPoolParallelism());
10+
11+
testForEach();
12+
testSearch();
13+
testReduce();
14+
}
15+
16+
private static void testReduce() {
17+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
18+
map.putIfAbsent("foo", "bar");
19+
map.putIfAbsent("han", "solo");
20+
map.putIfAbsent("r2", "d2");
21+
map.putIfAbsent("c3", "p0");
22+
23+
String reduced = map.reduce(1, (key, value) -> key + "=" + value,
24+
(s1, s2) -> s1 + ", " + s2);
25+
26+
System.out.println(reduced);
27+
}
28+
29+
private static void testSearch() {
30+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
31+
map.putIfAbsent("foo", "bar");
32+
map.putIfAbsent("han", "solo");
33+
map.putIfAbsent("r2", "d2");
34+
map.putIfAbsent("c3", "p0");
35+
36+
System.out.println("\nsearch()\n");
37+
38+
String result1 = map.search(1, (key, value) -> {
39+
System.out.println(Thread.currentThread().getName());
40+
if (key.equals("foo") && value.equals("bar")) {
41+
return "foobar";
42+
}
43+
return null;
44+
});
45+
46+
System.out.println(result1);
47+
48+
System.out.println("\nsearchValues()\n");
49+
50+
String result2 = map.searchValues(1, value -> {
51+
System.out.println(Thread.currentThread().getName());
52+
if (value.length() > 3) {
53+
return value;
54+
}
55+
return null;
56+
});
57+
58+
System.out.println(result2);
59+
}
60+
61+
private static void testForEach() {
62+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
63+
map.putIfAbsent("foo", "bar");
64+
map.putIfAbsent("han", "solo");
65+
map.putIfAbsent("r2", "d2");
66+
map.putIfAbsent("c3", "p0");
67+
68+
map.forEach(1, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
69+
// map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
70+
71+
System.out.println(map.mappingCount());
72+
}
73+
74+
}

java8-concurrent/src/main/java/io/github/biezhi/java8/concurrent/ConcurrentMapExample.java

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.biezhi.java8.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.TimeUnit;
5+
6+
/**
7+
* 并发工具类
8+
*
9+
* @author biezhi
10+
* @date 2018/3/11
11+
*/
12+
public class ConcurrentUtils {
13+
14+
public static void stop(ExecutorService executor) {
15+
try {
16+
executor.shutdown();
17+
executor.awaitTermination(60, TimeUnit.SECONDS);
18+
}
19+
catch (InterruptedException e) {
20+
System.err.println("termination interrupted");
21+
}
22+
finally {
23+
if (!executor.isTerminated()) {
24+
System.err.println("killing non-finished tasks");
25+
}
26+
executor.shutdownNow();
27+
}
28+
}
29+
30+
public static void sleep(int seconds) {
31+
try {
32+
TimeUnit.SECONDS.sleep(seconds);
33+
} catch (InterruptedException e) {
34+
throw new IllegalStateException(e);
35+
}
36+
}
37+
38+
39+
}

java8-concurrent/src/main/java/io/github/biezhi/java8/concurrent/ExecutorExample.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.biezhi.java8.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.TimeUnit;
6+
7+
public class Executors1 {
8+
9+
public static void main(String[] args) {
10+
test1(3);
11+
// test1(7);
12+
}
13+
14+
private static void test1(long seconds) {
15+
ExecutorService executor = Executors.newSingleThreadExecutor();
16+
executor.submit(() -> {
17+
try {
18+
TimeUnit.SECONDS.sleep(seconds);
19+
String name = Thread.currentThread().getName();
20+
System.out.println("task finished: " + name);
21+
} catch (InterruptedException e) {
22+
System.err.println("task interrupted");
23+
}
24+
});
25+
stop(executor);
26+
}
27+
28+
static void stop(ExecutorService executor) {
29+
try {
30+
System.out.println("attempt to shutdown executor");
31+
executor.shutdown();
32+
executor.awaitTermination(5, TimeUnit.SECONDS);
33+
} catch (InterruptedException e) {
34+
System.err.println("termination interrupted");
35+
} finally {
36+
if (!executor.isTerminated()) {
37+
System.err.println("killing non-finished tasks");
38+
}
39+
executor.shutdownNow();
40+
System.out.println("shutdown finished");
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)