Skip to content

Commit 85a8835

Browse files
committed
feat: 更新 java 示例
1 parent d4a6433 commit 85a8835

19 files changed

+557
-123
lines changed

codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/CallableDemo.java

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

codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/FutureDemo.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.dunwu.javacore.concurrent;
22

3+
import java.util.concurrent.Callable;
34
import java.util.concurrent.ExecutionException;
45
import java.util.concurrent.ExecutorService;
56
import java.util.concurrent.Executors;
@@ -14,7 +15,7 @@ public class FutureDemo {
1415

1516
public static void main(String[] args) {
1617
ExecutorService executor = Executors.newCachedThreadPool();
17-
CallableDemo task = new CallableDemo();
18+
Task task = new Task();
1819
Future<Integer> result = executor.submit(task);
1920
executor.shutdown();
2021

@@ -35,4 +36,19 @@ public static void main(String[] args) {
3536
System.out.println("所有任务执行完毕");
3637
}
3738

39+
static class Task implements Callable<Integer> {
40+
41+
@Override
42+
public Integer call() throws Exception {
43+
System.out.println("子线程在进行计算");
44+
Thread.sleep(3000);
45+
int sum = 0;
46+
for (int i = 0; i < 100; i++) {
47+
sum += i;
48+
}
49+
return sum;
50+
}
51+
52+
}
53+
3854
}

codes/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/FutureTaskDemo.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.dunwu.javacore.concurrent.error;
2+
3+
import io.github.dunwu.javacore.concurrent.annotation.NotThreadSafe;
4+
5+
@NotThreadSafe
6+
public class NotThreadSafeCounter {
7+
8+
private static long count = 0;
9+
10+
private void add() {
11+
int cnt = 0;
12+
while (cnt++ < 100000) {
13+
count += 1;
14+
}
15+
}
16+
17+
public static void main(String[] args) throws InterruptedException {
18+
final NotThreadSafeCounter demo = new NotThreadSafeCounter();
19+
// 创建两个线程,执行 add() 操作
20+
Thread t1 = new Thread(() -> {
21+
demo.add();
22+
});
23+
Thread t2 = new Thread(() -> {
24+
demo.add();
25+
});
26+
// 启动两个线程
27+
t1.start();
28+
t2.start();
29+
// 等待两个线程执行结束
30+
t1.join();
31+
t2.join();
32+
System.out.println("count = " + count);
33+
}
34+
35+
}
36+
// 输出:
37+
// count = 156602
38+
// 实际结果远小于预期值 200000
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.dunwu.javacore.concurrent.error;
2+
3+
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe;
4+
5+
@ThreadSafe
6+
public class ThreadSafeCounter {
7+
8+
private static long count = 0;
9+
10+
private synchronized void add() {
11+
int cnt = 0;
12+
while (cnt++ < 100000) {
13+
count += 1;
14+
}
15+
}
16+
17+
public static void main(String[] args) throws InterruptedException {
18+
final ThreadSafeCounter demo = new ThreadSafeCounter();
19+
// 创建两个线程,执行 add() 操作
20+
Thread t1 = new Thread(() -> {
21+
demo.add();
22+
});
23+
Thread t2 = new Thread(() -> {
24+
demo.add();
25+
});
26+
// 启动两个线程
27+
t1.start();
28+
t2.start();
29+
// 等待两个线程执行结束
30+
t1.join();
31+
t2.join();
32+
System.out.println("count = " + count);
33+
}
34+
35+
}
36+
// 输出:
37+
// count = 200000
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.dunwu.javacore.concurrent.jmm;
2+
3+
import io.github.dunwu.javacore.concurrent.annotation.NotThreadSafe;
4+
5+
/**
6+
* 双重检查锁
7+
* <p/>
8+
* Double-checked-locking antipattern
9+
*
10+
* @author Brian Goetz and Tim Peierls
11+
*/
12+
@NotThreadSafe
13+
public class DoubleCheckedLocking {
14+
15+
private static Resource resource;
16+
17+
public static Resource getInstance() {
18+
if (resource == null) {
19+
synchronized (DoubleCheckedLocking.class) {
20+
if (resource == null) { resource = new Resource(); }
21+
}
22+
}
23+
return resource;
24+
}
25+
26+
static class Resource { }
27+
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.dunwu.javacore.concurrent.jmm;
2+
3+
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe;
4+
5+
/**
6+
* 饿汉加载初始化(提前加载)
7+
* <p/>
8+
* Eager initialization
9+
*
10+
* @author Brian Goetz and Tim Peierls
11+
*/
12+
@ThreadSafe
13+
public class EagerInitialization {
14+
15+
private static Resource resource = new Resource();
16+
17+
public static Resource getResource() {
18+
return resource;
19+
}
20+
21+
static class Resource { }
22+
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.github.dunwu.javacore.concurrent.jmm;
2+
3+
/**
4+
* PossibleReordering
5+
* <p/>
6+
* Insufficiently synchronized program that can have surprising results
7+
*
8+
* @author Brian Goetz and Tim Peierls
9+
*/
10+
public class PossibleReordering {
11+
12+
static int x = 0, y = 0;
13+
static int a = 0, b = 0;
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
Thread one = new Thread(() -> {
17+
a = 1;
18+
x = b;
19+
});
20+
Thread other = new Thread(() -> {
21+
b = 1;
22+
y = a;
23+
});
24+
one.start();
25+
other.start();
26+
one.join();
27+
other.join();
28+
System.out.println("( " + x + ", " + y + " )");
29+
}
30+
31+
}
32+
// 输出:
33+
// 每次运行结果都不一样,例如:( 0, 1 )、( 1, 0 )、( 1, 1 )
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.dunwu.javacore.concurrent.jmm;
2+
3+
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe;
4+
5+
/**
6+
* 懒加载初始化(延迟加载)
7+
* <p/>
8+
* Thread-safe lazy initialization
9+
*
10+
* @author Brian Goetz and Tim Peierls
11+
*/
12+
@ThreadSafe
13+
public class SafeLazyInitialization {
14+
15+
private static Resource resource;
16+
17+
public synchronized static Resource getInstance() {
18+
if (resource == null) { resource = new Resource(); }
19+
return resource;
20+
}
21+
22+
static class Resource { }
23+
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.github.dunwu.javacore.concurrent.jmm;
2+
3+
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 不可变对象的初始化安全
10+
* <p/>
11+
* Initialization safety for immutable objects
12+
*
13+
* @author Brian Goetz and Tim Peierls
14+
*/
15+
@ThreadSafe
16+
public class SafeStates {
17+
18+
private final Map<String, String> states;
19+
20+
public SafeStates() {
21+
states = new HashMap<>();
22+
states.put("alaska", "AK");
23+
states.put("alabama", "AL");
24+
// ...
25+
states.put("wyoming", "WY");
26+
}
27+
28+
public String getAbbreviation(String s) {
29+
return states.get(s);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)