-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
557 additions
and
123 deletions.
There are no files selected for viewing
21 changes: 0 additions & 21 deletions
21
...s/javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/CallableDemo.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
...javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/FutureTaskDemo.java
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
...current/src/main/java/io/github/dunwu/javacore/concurrent/error/NotThreadSafeCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.dunwu.javacore.concurrent.error; | ||
|
||
import io.github.dunwu.javacore.concurrent.annotation.NotThreadSafe; | ||
|
||
@NotThreadSafe | ||
public class NotThreadSafeCounter { | ||
|
||
private static long count = 0; | ||
|
||
private void add() { | ||
int cnt = 0; | ||
while (cnt++ < 100000) { | ||
count += 1; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
final NotThreadSafeCounter demo = new NotThreadSafeCounter(); | ||
// 创建两个线程,执行 add() 操作 | ||
Thread t1 = new Thread(() -> { | ||
demo.add(); | ||
}); | ||
Thread t2 = new Thread(() -> { | ||
demo.add(); | ||
}); | ||
// 启动两个线程 | ||
t1.start(); | ||
t2.start(); | ||
// 等待两个线程执行结束 | ||
t1.join(); | ||
t2.join(); | ||
System.out.println("count = " + count); | ||
} | ||
|
||
} | ||
// 输出: | ||
// count = 156602 | ||
// 实际结果远小于预期值 200000 |
37 changes: 37 additions & 0 deletions
37
...concurrent/src/main/java/io/github/dunwu/javacore/concurrent/error/ThreadSafeCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.github.dunwu.javacore.concurrent.error; | ||
|
||
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe; | ||
|
||
@ThreadSafe | ||
public class ThreadSafeCounter { | ||
|
||
private static long count = 0; | ||
|
||
private synchronized void add() { | ||
int cnt = 0; | ||
while (cnt++ < 100000) { | ||
count += 1; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
final ThreadSafeCounter demo = new ThreadSafeCounter(); | ||
// 创建两个线程,执行 add() 操作 | ||
Thread t1 = new Thread(() -> { | ||
demo.add(); | ||
}); | ||
Thread t2 = new Thread(() -> { | ||
demo.add(); | ||
}); | ||
// 启动两个线程 | ||
t1.start(); | ||
t2.start(); | ||
// 等待两个线程执行结束 | ||
t1.join(); | ||
t2.join(); | ||
System.out.println("count = " + count); | ||
} | ||
|
||
} | ||
// 输出: | ||
// count = 200000 |
28 changes: 28 additions & 0 deletions
28
...oncurrent/src/main/java/io/github/dunwu/javacore/concurrent/jmm/DoubleCheckedLocking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.dunwu.javacore.concurrent.jmm; | ||
|
||
import io.github.dunwu.javacore.concurrent.annotation.NotThreadSafe; | ||
|
||
/** | ||
* 双重检查锁 | ||
* <p/> | ||
* Double-checked-locking antipattern | ||
* | ||
* @author Brian Goetz and Tim Peierls | ||
*/ | ||
@NotThreadSafe | ||
public class DoubleCheckedLocking { | ||
|
||
private static Resource resource; | ||
|
||
public static Resource getInstance() { | ||
if (resource == null) { | ||
synchronized (DoubleCheckedLocking.class) { | ||
if (resource == null) { resource = new Resource(); } | ||
} | ||
} | ||
return resource; | ||
} | ||
|
||
static class Resource { } | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...concurrent/src/main/java/io/github/dunwu/javacore/concurrent/jmm/EagerInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.github.dunwu.javacore.concurrent.jmm; | ||
|
||
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe; | ||
|
||
/** | ||
* 饿汉加载初始化(提前加载) | ||
* <p/> | ||
* Eager initialization | ||
* | ||
* @author Brian Goetz and Tim Peierls | ||
*/ | ||
@ThreadSafe | ||
public class EagerInitialization { | ||
|
||
private static Resource resource = new Resource(); | ||
|
||
public static Resource getResource() { | ||
return resource; | ||
} | ||
|
||
static class Resource { } | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/jmm/PossibleReordering.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.github.dunwu.javacore.concurrent.jmm; | ||
|
||
/** | ||
* PossibleReordering | ||
* <p/> | ||
* Insufficiently synchronized program that can have surprising results | ||
* | ||
* @author Brian Goetz and Tim Peierls | ||
*/ | ||
public class PossibleReordering { | ||
|
||
static int x = 0, y = 0; | ||
static int a = 0, b = 0; | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Thread one = new Thread(() -> { | ||
a = 1; | ||
x = b; | ||
}); | ||
Thread other = new Thread(() -> { | ||
b = 1; | ||
y = a; | ||
}); | ||
one.start(); | ||
other.start(); | ||
one.join(); | ||
other.join(); | ||
System.out.println("( " + x + ", " + y + " )"); | ||
} | ||
|
||
} | ||
// 输出: | ||
// 每次运行结果都不一样,例如:( 0, 1 )、( 1, 0 )、( 1, 1 ) |
24 changes: 24 additions & 0 deletions
24
...current/src/main/java/io/github/dunwu/javacore/concurrent/jmm/SafeLazyInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.dunwu.javacore.concurrent.jmm; | ||
|
||
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe; | ||
|
||
/** | ||
* 懒加载初始化(延迟加载) | ||
* <p/> | ||
* Thread-safe lazy initialization | ||
* | ||
* @author Brian Goetz and Tim Peierls | ||
*/ | ||
@ThreadSafe | ||
public class SafeLazyInitialization { | ||
|
||
private static Resource resource; | ||
|
||
public synchronized static Resource getInstance() { | ||
if (resource == null) { resource = new Resource(); } | ||
return resource; | ||
} | ||
|
||
static class Resource { } | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...javacore-concurrent/src/main/java/io/github/dunwu/javacore/concurrent/jmm/SafeStates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.github.dunwu.javacore.concurrent.jmm; | ||
|
||
import io.github.dunwu.javacore.concurrent.annotation.ThreadSafe; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* 不可变对象的初始化安全 | ||
* <p/> | ||
* Initialization safety for immutable objects | ||
* | ||
* @author Brian Goetz and Tim Peierls | ||
*/ | ||
@ThreadSafe | ||
public class SafeStates { | ||
|
||
private final Map<String, String> states; | ||
|
||
public SafeStates() { | ||
states = new HashMap<>(); | ||
states.put("alaska", "AK"); | ||
states.put("alabama", "AL"); | ||
// ... | ||
states.put("wyoming", "WY"); | ||
} | ||
|
||
public String getAbbreviation(String s) { | ||
return states.get(s); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...rrent/src/main/java/io/github/dunwu/javacore/concurrent/jmm/UnsafeLazyInitialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.dunwu.javacore.concurrent.jmm; | ||
|
||
import io.github.dunwu.javacore.concurrent.annotation.NotThreadSafe; | ||
|
||
/** | ||
* UnsafeLazyInitialization | ||
* <p/> | ||
* Unsafe lazy initialization | ||
* | ||
* @author Brian Goetz and Tim Peierls | ||
*/ | ||
@NotThreadSafe | ||
public class UnsafeLazyInitialization { | ||
|
||
private static Resource resource; | ||
|
||
public static Resource getInstance() { | ||
if (resource == null) { | ||
resource = new Resource(); // unsafe publication | ||
} | ||
return resource; | ||
} | ||
|
||
static class Resource { } | ||
|
||
} |
Oops, something went wrong.