Skip to content

Commit 538f89e

Browse files
committed
java并发
1 parent 3f371f1 commit 538f89e

14 files changed

Lines changed: 913 additions & 114 deletions

File tree

code/Java/java-concurrency/src/main/java/com/heibaiying/atomic/J5_AtomicIntegerFieldUpdater.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static void main(String[] args) throws InterruptedException {
3131
CountDownLatch latch = new CountDownLatch(number);
3232
ExecutorService executorService = Executors.newFixedThreadPool(10);
3333
Candidate candidate = new Candidate("候选人", 0);
34+
// 使用字段更新型来保证其线程安全
3435
AtomicIntegerFieldUpdater<Candidate> fieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Candidate.class, "score");
3536
for (int i = 0; i < number; i++) {
3637
executorService.execute(new Task(latch, candidate, fieldUpdater));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.heibaiying.createThread;
2+
3+
import java.util.concurrent.Callable;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.FutureTask;
6+
7+
public class J3_Method03 {
8+
public static void main(String[] args) throws ExecutionException, InterruptedException {
9+
Task task = new Task();
10+
FutureTask<Integer> futureTask = new FutureTask<>(task);
11+
new Thread(futureTask).start();
12+
System.out.println("获得线程返回值:" + futureTask.get());
13+
}
14+
}
15+
16+
class Task implements Callable<Integer> {
17+
18+
@Override
19+
public Integer call() {
20+
return 100;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.heibaiying.future;
2+
3+
import java.util.concurrent.*;
4+
5+
public class J0_Callable {
6+
7+
static class Task implements Callable<Integer> {
8+
9+
@Override
10+
public Integer call() throws InterruptedException {
11+
Thread.sleep(3000);
12+
return 100;
13+
}
14+
}
15+
16+
public static void main(String[] args) throws ExecutionException, InterruptedException {
17+
ExecutorService executors = Executors.newSingleThreadExecutor();
18+
Future<Integer> future = executors.submit(new Task());
19+
System.out.println("计算结果为:" + future.get());
20+
executors.shutdown();
21+
}
22+
}

code/Java/java-concurrency/src/main/java/com/heibaiying/future/J1_Future.java

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.heibaiying.future;
2+
3+
import java.util.concurrent.*;
4+
5+
public class J1_FutureTask {
6+
7+
static class Task implements Callable<Integer> {
8+
9+
@Override
10+
public Integer call() throws InterruptedException {
11+
Thread.sleep(3000);
12+
return 100;
13+
}
14+
}
15+
16+
public static void main(String[] args) throws ExecutionException, InterruptedException {
17+
FutureTask<Integer> futureTask01 = new FutureTask<>(new Task());
18+
FutureTask<Integer> futureTask02 = new FutureTask<>(new Task());
19+
new Thread(futureTask01).start();
20+
ExecutorService executorService = Executors.newSingleThreadExecutor();
21+
executorService.submit(futureTask02);
22+
System.out.println("futureTask01 计算结果为:" + futureTask01.get());
23+
System.out.println("futureTask02 计算结果为:" + futureTask01.get());
24+
executorService.shutdown();
25+
}
26+
}

code/Java/java-concurrency/src/main/java/com/heibaiying/future/J2_CompletableFuture.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ public void run() {
2626
}
2727

2828
public static void main(String[] args) throws InterruptedException {
29+
int intermediateResult;
2930
CompletableFuture<Integer> future = new CompletableFuture<>();
30-
System.out.println("主线程开始计算");
31+
// 启动子线程
3132
new Thread(new Compute(future)).start();
32-
int i = 0;
33-
for (int j = 0; j < 100; j++) {
34-
i = i + j;
35-
}
33+
System.out.println("启动主线程");
3634
Thread.sleep(2000);
3735
System.out.println("主线程计算完成");
38-
future.complete(i);
36+
// 假设主线程计算结果为 100
37+
intermediateResult = 100;
38+
// 传递主线程的计算结果给子线程
39+
future.complete(intermediateResult);
3940
}
4041
}

code/Java/java-concurrency/src/main/java/com/heibaiying/future/J3_SupplyAsync.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
public class J3_SupplyAsync {
77

88
private static Integer compute() {
9-
int i = 0;
10-
for (int j = 0; j < 100; j++) {
11-
i = i + j;
12-
}
139
try {
1410
Thread.sleep(2000);
1511
} catch (InterruptedException e) {
1612
e.printStackTrace();
1713
}
1814
System.out.println("子线程计算完成");
19-
return i;
15+
return 100;
2016
}
2117

2218
public static void main(String[] args) throws ExecutionException, InterruptedException {

code/Java/java-concurrency/src/main/java/com/heibaiying/future/J4_StreamingCall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
3838
.thenApply(J4_StreamingCall::multi)
3939
.thenAccept(J4_StreamingCall::accept) //值在这一步被消费掉了
4040
.thenAccept(x -> System.out.println("运算结果:" + x));
41-
future.get(); //惰性求值,如果缺少这一步,不会有任何输出
41+
future.get(); //类似于流式计算的惰性求值,如果缺少这一步,不会有任何输出
4242
}
4343
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.heibaiying.lockOptimization;
2+
3+
import java.util.Date;
4+
5+
public class Employee {
6+
private String name;
7+
private int age;
8+
private Date birthday;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public int getAge() {
19+
return age;
20+
}
21+
22+
public void setAge(int age) {
23+
this.age = age;
24+
}
25+
26+
public Date getBirthday() {
27+
return birthday;
28+
}
29+
30+
public void setBirthday(Date birthday) {
31+
this.birthday = birthday;
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.heibaiying.lockOptimization;
2+
3+
/**
4+
* 锁消除
5+
*/
6+
public class LockElision {
7+
8+
private String toJson(Employee employee) {
9+
StringBuffer buffer = new StringBuffer();
10+
buffer.append("name:").append(employee.getName());
11+
buffer.append("age:").append(employee.getAge());
12+
buffer.append("birthday:").append(employee.getBirthday());
13+
return buffer.toString();
14+
}
15+
}
16+
17+

0 commit comments

Comments
 (0)