Skip to content

Commit 257b80d

Browse files
committed
add java8
1 parent 80f5f0a commit 257b80d

68 files changed

Lines changed: 3618 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cn.byhieg.designpatterntutorial.builder;
2+
3+
public class ChDong {
4+
private int age ;
5+
private boolean gender;
6+
private String name;
7+
8+
9+
public static void main(String[] args) {
10+
ChDong cd = new ChDong.ChDongBuilder()
11+
.withAge(10)
12+
.withGender(true)
13+
.build();
14+
15+
}
16+
public static final class ChDongBuilder {
17+
int age;
18+
boolean gender;
19+
String name;
20+
21+
private ChDongBuilder() {
22+
}
23+
24+
public static ChDongBuilder aChDong() {
25+
return new ChDongBuilder();
26+
}
27+
28+
public ChDongBuilder withAge(int age) {
29+
this.age = age;
30+
return this;
31+
}
32+
33+
public ChDongBuilder withGender(boolean gender) {
34+
this.gender = gender;
35+
return this;
36+
}
37+
38+
public ChDongBuilder withName(String name) {
39+
this.name = name;
40+
return this;
41+
}
42+
43+
public ChDong build() {
44+
ChDong chDong = new ChDong();
45+
chDong.gender = this.gender;
46+
chDong.age = this.age;
47+
chDong.name = this.name;
48+
return chDong;
49+
}
50+
}
51+
52+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.byhieg.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+
* @author Benjamin Winterberg
10+
*/
11+
public class Atomic1 {
12+
13+
private static final int NUM_INCREMENTS = 1000;
14+
15+
private static AtomicInteger atomicInt = new AtomicInteger(0);
16+
17+
public static void main(String[] args) {
18+
testIncrement();
19+
testAccumulate();
20+
testUpdate();
21+
}
22+
23+
private static void testUpdate() {
24+
atomicInt.set(0);
25+
26+
ExecutorService executor = Executors.newFixedThreadPool(2);
27+
28+
IntStream.range(0, NUM_INCREMENTS)
29+
.forEach(i -> {
30+
Runnable task = () ->
31+
atomicInt.updateAndGet(n -> n + 2);
32+
executor.submit(task);
33+
});
34+
35+
ConcurrentUtils.stop(executor);
36+
37+
System.out.format("Update: %d\n", atomicInt.get());
38+
}
39+
40+
private static void testAccumulate() {
41+
atomicInt.set(0);
42+
43+
ExecutorService executor = Executors.newFixedThreadPool(2);
44+
45+
IntStream.range(0, NUM_INCREMENTS)
46+
.forEach(i -> {
47+
Runnable task = () ->
48+
atomicInt.accumulateAndGet(i, (n, m) -> n + m);
49+
executor.submit(task);
50+
});
51+
52+
ConcurrentUtils.stop(executor);
53+
54+
System.out.format("Accumulate: %d\n", atomicInt.get());
55+
}
56+
57+
private static void testIncrement() {
58+
atomicInt.set(0);
59+
60+
ExecutorService executor = Executors.newFixedThreadPool(2);
61+
62+
IntStream.range(0, NUM_INCREMENTS)
63+
.forEach(i -> executor.submit(atomicInt::incrementAndGet));
64+
65+
ConcurrentUtils.stop(executor);
66+
67+
System.out.format("Increment: Expected=%d; Is=%d\n", NUM_INCREMENTS, atomicInt.get());
68+
}
69+
70+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.byhieg.java8.concurrent;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
public class CompletableFuture1 {
10+
11+
public static void main(String[] args) throws ExecutionException, InterruptedException {
12+
CompletableFuture<String> future = new CompletableFuture<>();
13+
14+
future.complete("42");
15+
16+
future
17+
.thenAccept(System.out::println)
18+
.thenAccept(v -> System.out.println("done"));
19+
20+
}
21+
22+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cn.byhieg.java8.concurrent;
2+
3+
import java.util.concurrent.ConcurrentHashMap;
4+
import java.util.concurrent.ForkJoinPool;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
public class ConcurrentHashMap1 {
10+
11+
public static void main(String[] args) {
12+
System.out.println("Parallelism: " + ForkJoinPool.getCommonPoolParallelism());
13+
14+
testForEach();
15+
testSearch();
16+
testReduce();
17+
}
18+
19+
private static void testReduce() {
20+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
21+
map.putIfAbsent("foo", "bar");
22+
map.putIfAbsent("han", "solo");
23+
map.putIfAbsent("r2", "d2");
24+
map.putIfAbsent("c3", "p0");
25+
26+
String reduced = map.reduce(1, (key, value) -> key + "=" + value,
27+
(s1, s2) -> s1 + ", " + s2);
28+
29+
System.out.println(reduced);
30+
}
31+
32+
private static void testSearch() {
33+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
34+
map.putIfAbsent("foo", "bar");
35+
map.putIfAbsent("han", "solo");
36+
map.putIfAbsent("r2", "d2");
37+
map.putIfAbsent("c3", "p0");
38+
39+
System.out.println("\nsearch()\n");
40+
41+
String result1 = map.search(1, (key, value) -> {
42+
System.out.println(Thread.currentThread().getName());
43+
if (key.equals("foo") && value.equals("bar")) {
44+
return "foobar";
45+
}
46+
return null;
47+
});
48+
49+
System.out.println(result1);
50+
51+
System.out.println("\nsearchValues()\n");
52+
53+
String result2 = map.searchValues(1, value -> {
54+
System.out.println(Thread.currentThread().getName());
55+
if (value.length() > 3) {
56+
return value;
57+
}
58+
return null;
59+
});
60+
61+
System.out.println(result2);
62+
}
63+
64+
private static void testForEach() {
65+
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
66+
map.putIfAbsent("foo", "bar");
67+
map.putIfAbsent("han", "solo");
68+
map.putIfAbsent("r2", "d2");
69+
map.putIfAbsent("c3", "p0");
70+
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()));
73+
74+
System.out.println(map.mappingCount());
75+
}
76+
77+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.byhieg.java8.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.TimeUnit;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
public class ConcurrentUtils {
10+
11+
public static void stop(ExecutorService executor) {
12+
try {
13+
executor.shutdown();
14+
executor.awaitTermination(60, TimeUnit.SECONDS);
15+
}
16+
catch (InterruptedException e) {
17+
System.err.println("termination interrupted");
18+
}
19+
finally {
20+
if (!executor.isTerminated()) {
21+
System.err.println("killing non-finished tasks");
22+
}
23+
executor.shutdownNow();
24+
}
25+
}
26+
27+
public static void sleep(int seconds) {
28+
try {
29+
TimeUnit.SECONDS.sleep(seconds);
30+
} catch (InterruptedException e) {
31+
throw new IllegalStateException(e);
32+
}
33+
}
34+
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.byhieg.java8.concurrent;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author Benjamin Winterberg
9+
*/
10+
public class Executors1 {
11+
12+
public static void main(String[] args) {
13+
test1(3);
14+
// test1(7);
15+
}
16+
17+
private static void test1(long seconds) {
18+
ExecutorService executor = Executors.newSingleThreadExecutor();
19+
executor.submit(() -> {
20+
try {
21+
TimeUnit.SECONDS.sleep(seconds);
22+
String name = Thread.currentThread().getName();
23+
System.out.println("task finished: " + name);
24+
}
25+
catch (InterruptedException e) {
26+
System.err.println("task interrupted");
27+
}
28+
});
29+
stop(executor);
30+
}
31+
32+
static void stop(ExecutorService executor) {
33+
try {
34+
System.out.println("attempt to shutdown executor");
35+
executor.shutdown();
36+
executor.awaitTermination(5, TimeUnit.SECONDS);
37+
}
38+
catch (InterruptedException e) {
39+
System.err.println("termination interrupted");
40+
}
41+
finally {
42+
if (!executor.isTerminated()) {
43+
System.err.println("killing non-finished tasks");
44+
}
45+
executor.shutdownNow();
46+
System.out.println("shutdown finished");
47+
}
48+
}
49+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cn.byhieg.java8.concurrent;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.Future;
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.concurrent.TimeoutException;
9+
10+
/**
11+
* @author Benjamin Winterberg
12+
*/
13+
public class Executors2 {
14+
15+
public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
16+
test1();
17+
// test2();
18+
// test3();
19+
}
20+
21+
private static void test3() throws InterruptedException, ExecutionException, TimeoutException {
22+
ExecutorService executor = Executors.newFixedThreadPool(1);
23+
24+
Future<Integer> future = executor.submit(() -> {
25+
try {
26+
TimeUnit.SECONDS.sleep(2);
27+
return 123;
28+
}
29+
catch (InterruptedException e) {
30+
throw new IllegalStateException("task interrupted", e);
31+
}
32+
});
33+
future.get(1, TimeUnit.SECONDS);
34+
}
35+
36+
private static void test2() throws InterruptedException, ExecutionException {
37+
ExecutorService executor = Executors.newFixedThreadPool(1);
38+
39+
Future<Integer> future = executor.submit(() -> {
40+
try {
41+
TimeUnit.SECONDS.sleep(1);
42+
return 123;
43+
}
44+
catch (InterruptedException e) {
45+
throw new IllegalStateException("task interrupted", e);
46+
}
47+
});
48+
49+
executor.shutdownNow();
50+
future.get();
51+
}
52+
53+
private static void test1() throws InterruptedException, ExecutionException {
54+
ExecutorService executor = Executors.newFixedThreadPool(1);
55+
56+
Future<Integer> future = executor.submit(() -> {
57+
try {
58+
TimeUnit.SECONDS.sleep(2);
59+
return 123;
60+
}
61+
catch (InterruptedException e) {
62+
throw new IllegalStateException("task interrupted", e);
63+
}
64+
});
65+
66+
67+
68+
while (!future.isDone()) {
69+
System.out.println("future done: " + future.isDone());
70+
// Integer result = future.get();
71+
}
72+
73+
Integer result = future.get();
74+
75+
System.out.println("future done: " + future.isDone());
76+
System.out.print("result: " + result);
77+
78+
executor.shutdownNow();
79+
}
80+
81+
}

0 commit comments

Comments
 (0)