Skip to content

Commit 94c24c6

Browse files
committed
Add Stage3 Lesson 4
1 parent 4cab64e commit 94c24c6

File tree

15 files changed

+539
-2
lines changed

15 files changed

+539
-2
lines changed

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/stage-2/lesson-4/src/main/java/com/segmentfault/deep/in/java/collection/algorithm/QuickSort.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.segmentfault.deep.in.java.collection.algorithm;
22

3+
import java.util.Arrays;
34
import java.util.stream.Stream;
45

56
public class QuickSort<T extends Comparable<T>> implements Sort<T> {
@@ -82,9 +83,9 @@ int partition(T[] values, int low, int high) {
8283
}
8384

8485
public static void main(String[] args) {
85-
Integer[] values = Sort.of(3, 1, 2, 5, 4);
86+
Integer[] values = Sort.of(2, 5, 6, 7, 8, 8, 9, 2, 1, 6, 7, 5, 6, 11, 23);
8687
Sort<Integer> sort = new QuickSort<>(); // Java 7 Diamond 语法
8788
sort.sort(values);
88-
Stream.of(values).forEach(System.out::println);
89+
System.out.println(Arrays.asList(values));
8990
}
9091
}

「一入 Java 深似海 」/代码/segmentfault/deep-in-java/stage-3/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>stage-3-lesson-1</module>
1616
<module>stage-3-lesson-2</module>
1717
<module>stage-3-lesson-3</module>
18+
<module>stage-3-lesson-4</module>
1819
</modules>
1920
<packaging>pom</packaging>
2021

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>stage-3</artifactId>
7+
<groupId>com.segmentfault</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>stage-3-lesson-4</artifactId>
13+
<name>「一入 Java 深似海 」系列 :: 第三期 :: 第四节</name>
14+
<url>https://segmentfault.com/ls/1650000018320970/l/1500000018320681</url>
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
import java.util.concurrent.*;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
public class ArrayBlockingQueueConcurrentDemo {
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
10+
// 最大允许添加 2 个元素
11+
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(2, true);
12+
// 申请 2 个大小的线程池
13+
ExecutorService executorService = Executors.newFixedThreadPool(2);
14+
15+
for (AtomicInteger i = new AtomicInteger(1); i.get() < 100; i.incrementAndGet()) {
16+
executorService.submit(() -> { // 写线程(1)
17+
try {
18+
queue.put(i.get());
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
});
23+
24+
executorService.submit(() -> { // 读线程(1)
25+
try {
26+
System.out.println(queue.take());
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
});
31+
}
32+
33+
// executorService.submit(() -> { // 写线程(2)
34+
// queue.put(2);
35+
// });
36+
37+
38+
executorService.awaitTermination(10, TimeUnit.SECONDS);
39+
// 关闭线程池
40+
executorService.shutdown();
41+
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
import java.util.concurrent.ArrayBlockingQueue;
4+
import java.util.concurrent.BlockingQueue;
5+
6+
public class ArrayBlockingQueueDemo {
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
10+
// ArrayBlockingQueue 是有限队列
11+
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue(3);
12+
13+
// Queue 添加操作中 offer 安全性高于 add 方法
14+
// demoOfferMethod(queue);
15+
// demoAddMethod(queue);
16+
17+
demoPutMethod(queue);
18+
// BlockingQueue 要使用 put 方法多于 offer 方法
19+
20+
System.out.println(queue);
21+
}
22+
23+
private static void demoPutMethod(BlockingQueue<Integer> queue) throws InterruptedException {
24+
queue.put(1);
25+
queue.put(2);
26+
queue.put(3);
27+
// 如果超过了 capacity,?
28+
queue.put(4);
29+
}
30+
31+
private static void demoAddMethod(BlockingQueue<Integer> queue) {
32+
queue.add(1);
33+
queue.add(2);
34+
queue.add(3);
35+
// 如果超过了 capacity,throw new IllegalStateException("Queue full")
36+
// queue.add(4);
37+
}
38+
39+
private static void demoOfferMethod(BlockingQueue<Integer> queue) {
40+
queue.offer(1);
41+
queue.offer(2);
42+
queue.offer(3);
43+
// 如果超过了 capacity,后面的offer 会被忽略
44+
queue.offer(4);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
5+
public class CompletableFutureDemo {
6+
7+
public static void main(String[] args) {
8+
// CompletableFuture
9+
10+
CompletableFuture.supplyAsync(() -> {
11+
return 1;
12+
}).thenApply(String::valueOf)
13+
// 异常的方式结束
14+
.completeExceptionally(new RuntimeException())
15+
;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
public class ConcurrentHashMapDemo {
4+
5+
public static void main(String[] args) {
6+
7+
// ConcurrentHashMap 读多写少场景
8+
// 1.6 读(部分锁) -> 1.7 无锁(Segment) -> 1.8 无锁(红黑树——更好地解决 Hash 冲突)
9+
// ConcurrentSkipListMap 读多写多场景(如果内存足够时)
10+
11+
// ConcurrentHashMap
12+
// table 2^n ->
13+
// n = 1 -> size = 2
14+
// n = 2 -> size = 2^2 = 4
15+
// n = 3 -> size = 2^3 = 8 TREEIFY_THRESHOLD
16+
// log2(8) = 3
17+
// [0] -> [1] -> [2]
18+
// [0] -> [1]
19+
// -> [2]
20+
// n = 4 -> size = 2^4 = 16
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
import java.util.Iterator;
4+
import java.util.concurrent.CopyOnWriteArrayList;
5+
6+
public class CopyOnWriteArrayListDemo {
7+
8+
public static void main(String[] args) {
9+
CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>(); // Diamond 语法
10+
11+
// main 线程插入三条数据
12+
// 安装 Thread ID
13+
list.add(1);
14+
// 判断当前线程 ID == main.threadId
15+
list.add(2);
16+
list.add(3);
17+
// Copy
18+
// JDK 升级两大核心性能提升
19+
// 1. 数组
20+
// 2. String
21+
22+
// ConcurrentModificationException
23+
int times = 0;
24+
Iterator iterator = list.iterator();
25+
while (iterator.hasNext() && times < 100) {
26+
iterator.next();
27+
list.add(2);
28+
times++;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class HashMapDemo {
7+
8+
public static void main(String[] args) {
9+
10+
String strValue = "Hello,World";
11+
12+
Integer intValue = new Integer(strValue.hashCode());
13+
14+
Map<Object, Object> map = new HashMap<>();
15+
16+
map.put(strValue, 1);
17+
map.put(intValue, 2);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.segmentfault.deep.in.java.concurrency;
2+
3+
import java.util.concurrent.*;
4+
5+
public class Java9FlowDemo {
6+
7+
public static void main(String[] args) throws InterruptedException {
8+
9+
// Java 7 try-catch 语法
10+
11+
try (SubmissionPublisher<String> publisher = new SubmissionPublisher<>()) {
12+
13+
// 订阅
14+
publisher.subscribe(new Flow.Subscriber<String>() {
15+
16+
private Flow.Subscription subscription;
17+
18+
@Override
19+
public void onSubscribe(Flow.Subscription subscription) {
20+
this.subscription = subscription;
21+
println("已订阅");
22+
// 订阅无限(Long.MAX_VALUE)数据
23+
subscription.request(Long.MAX_VALUE);
24+
}
25+
26+
@Override
27+
public void onNext(String item) {
28+
if ("exit".equalsIgnoreCase(item)) {
29+
// 取消订阅
30+
subscription.cancel();
31+
return;
32+
} else if ("exception".equalsIgnoreCase(item)) {
33+
throw new RuntimeException("Throw an exception...");
34+
}
35+
println("得到数据:" + item);
36+
}
37+
38+
@Override
39+
public void onError(Throwable throwable) {
40+
println("得到异常:" + throwable);
41+
}
42+
43+
@Override
44+
public void onComplete() {
45+
println("操作完成");
46+
}
47+
});
48+
49+
// 发布者发布数据
50+
publisher.submit("Hello,World");
51+
publisher.submit("2019");
52+
53+
// 故意抛出异常
54+
publisher.submit("exception");
55+
56+
// exit 是退出命令
57+
publisher.submit("exit");
58+
// 当 exit 出现时,忽略后续提交
59+
publisher.submit("ABCDEFG");
60+
publisher.submit("HIGKLMN");
61+
62+
ExecutorService executor = (ExecutorService) publisher.getExecutor();
63+
64+
executor.awaitTermination(100, TimeUnit.MILLISECONDS);
65+
66+
}
67+
}
68+
69+
private static void println(Object object) {
70+
System.out.printf("[线程: %s] - %s\n", Thread.currentThread().getName(), object);
71+
}
72+
}

0 commit comments

Comments
 (0)