Skip to content

Commit

Permalink
feat: 更新 java 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
dunwu committed Sep 12, 2024
1 parent d4a6433 commit 85a8835
Show file tree
Hide file tree
Showing 19 changed files with 557 additions and 123 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.dunwu.javacore.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -14,7 +15,7 @@ public class FutureDemo {

public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
CallableDemo task = new CallableDemo();
Task task = new Task();
Future<Integer> result = executor.submit(task);
executor.shutdown();

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

static class Task implements Callable<Integer> {

@Override
public Integer call() throws Exception {
System.out.println("子线程在进行计算");
Thread.sleep(3000);
int sum = 0;
for (int i = 0; i < 100; i++) {
sum += i;
}
return sum;
}

}

}

This file was deleted.

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
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
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 { }

}
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 { }

}
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 )
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 { }

}
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);
}

}
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 { }

}
Loading

0 comments on commit 85a8835

Please sign in to comment.