Skip to content

Commit ca5f7f1

Browse files
committed
11.17 Starter作业
1 parent aa6a782 commit ca5f7f1

22 files changed

Lines changed: 641 additions & 11 deletions

File tree

03concurrency/0301/src/main/java/java0/conc0303/Homework03.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void main(String[] args) {
2727
// 然后退出main线程
2828
}
2929

30-
private static int sum() {
30+
static int sum() {
3131
return fibo(36);
3232
}
3333

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
package java0.conc0303;
2+
3+
import java.util.concurrent.*;
4+
import java.util.concurrent.atomic.AtomicBoolean;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
import java.util.concurrent.locks.Condition;
7+
import java.util.concurrent.locks.Lock;
8+
import java.util.concurrent.locks.ReentrantLock;
9+
import java.util.function.Supplier;
10+
11+
public class HomeworkDemo1 {
12+
public static void main(String[] args) {
13+
14+
// long start = System.currentTimeMillis();
15+
// // 在这里创建一个线程或线程池,
16+
// // 异步执行 下面方法
17+
//
18+
// int result = sum(); //这是得到的返回值
19+
//
20+
// // 确保 拿到result 并输出
21+
// System.out.println("异步计算结果为:" + result);
22+
//
23+
// System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms");
24+
//
25+
// // 然后退出main线程
26+
run(bySleep, "bySleep");
27+
28+
run(byJoin, "byJoin");
29+
30+
run(byCAS, "byCAS");
31+
32+
run(byLock, "byLock");
33+
34+
run(byObjectNotify, "byObjectNotify");
35+
36+
run(byCountDownLatch, "byCountDownLatch");
37+
38+
run(byFuture, "byFuture");
39+
40+
run(byFutureTask, "byFutureTask");
41+
42+
run(byCompletableFuture, "byCompletableFuture");
43+
}
44+
45+
public static void run(Supplier<Integer> supplier, String type) {
46+
System.out.println(type);
47+
long start = System.currentTimeMillis();
48+
// 在这里创建一个线程或线程池,
49+
// 异步执行 下面方法
50+
// 这是得到的返回值
51+
int result = supplier.get();
52+
53+
// 确保 拿到result 并输出
54+
System.out.print("异步计算结果为:" + result + " ");
55+
56+
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms");
57+
58+
// 然后退出main线程
59+
}
60+
61+
private static int sum() {
62+
return fibo(36);
63+
}
64+
65+
private static int fibo(int a) {
66+
if (a < 2) {
67+
return 1;
68+
}
69+
return fibo(a - 1) + fibo(a - 2);
70+
}
71+
72+
private static final Supplier<Integer> bySleep = () -> {
73+
ExecutorService executorService = Executors.newSingleThreadExecutor();
74+
AtomicInteger result = new AtomicInteger();
75+
executorService.execute(() -> result.set(sum()));
76+
try {
77+
Thread.sleep(1000);
78+
} catch (InterruptedException e) {
79+
e.printStackTrace();
80+
}
81+
executorService.shutdown();
82+
return result.get();
83+
};
84+
85+
private static final Supplier<Integer> byJoin = () -> {
86+
AtomicInteger result = new AtomicInteger();
87+
Thread thread = new Thread(() -> result.set(sum()));
88+
thread.start();
89+
try {
90+
thread.join();
91+
} catch (InterruptedException e) {
92+
e.printStackTrace();
93+
}
94+
return result.get();
95+
};
96+
97+
public static final Supplier<Integer> byCAS = () -> {
98+
ExecutorService executorService = Executors.newSingleThreadExecutor();
99+
AtomicBoolean isDone = new AtomicBoolean(false);
100+
AtomicInteger result = new AtomicInteger();
101+
executorService.execute(() -> {
102+
result.set(sum());
103+
isDone.set(true);
104+
});
105+
while (!isDone.get()) {
106+
}
107+
executorService.shutdown();
108+
return result.get();
109+
};
110+
111+
public static final Supplier<Integer> byLock = () -> {
112+
ExecutorService executorService = Executors.newSingleThreadExecutor();
113+
Lock lock = new ReentrantLock();
114+
Condition condition = lock.newCondition();
115+
AtomicInteger result = new AtomicInteger();
116+
AtomicBoolean isDone = new AtomicBoolean(false);
117+
executorService.execute(() -> {
118+
lock.lock();
119+
try {
120+
result.set(sum());
121+
isDone.set(true);
122+
condition.signalAll();
123+
} finally {
124+
lock.unlock();
125+
}
126+
});
127+
128+
lock.lock();
129+
try {
130+
while (!isDone.get()) {
131+
condition.await();
132+
}
133+
condition.signalAll();
134+
} catch (InterruptedException e) {
135+
e.printStackTrace();
136+
} finally {
137+
lock.unlock();
138+
}
139+
executorService.shutdown();
140+
return result.get();
141+
};
142+
143+
public static final Supplier<Integer> byObjectNotify = () -> {
144+
ExecutorService executorService = Executors.newSingleThreadExecutor();
145+
Object object = new Object();
146+
AtomicInteger result = new AtomicInteger();
147+
AtomicBoolean isDone = new AtomicBoolean(false);
148+
executorService.execute(() -> {
149+
synchronized (object) {
150+
result.set(sum());
151+
isDone.set(true);
152+
object.notifyAll();
153+
}
154+
});
155+
156+
synchronized (object) {
157+
while (!isDone.get()) {
158+
try {
159+
object.wait();
160+
} catch (InterruptedException e) {
161+
e.printStackTrace();
162+
}
163+
}
164+
}
165+
executorService.shutdown();
166+
return result.get();
167+
};
168+
169+
public static final Supplier<Integer> byCountDownLatch = () -> {
170+
ExecutorService executorService = Executors.newSingleThreadExecutor();
171+
CountDownLatch countDownLatch = new CountDownLatch(1);
172+
AtomicInteger result = new AtomicInteger();
173+
executorService.execute(() -> {
174+
result.set(sum());
175+
countDownLatch.countDown();
176+
});
177+
try {
178+
countDownLatch.await();
179+
} catch (InterruptedException e) {
180+
e.printStackTrace();
181+
}
182+
executorService.shutdown();
183+
return result.get();
184+
};
185+
186+
private static final Supplier<Integer> byFuture = () -> {
187+
ExecutorService executorService = Executors.newSingleThreadExecutor();
188+
Future<Integer> future = executorService.submit(()->sum());
189+
Integer result = null;
190+
try {
191+
result = future.get();
192+
} catch (InterruptedException | ExecutionException e) {
193+
e.printStackTrace();
194+
}
195+
executorService.shutdown();
196+
return result;
197+
};
198+
199+
private static final Supplier<Integer> byFutureTask = () -> {
200+
ExecutorService executorService = Executors.newSingleThreadExecutor();
201+
FutureTask<Integer> futureTask = new FutureTask<>(Homework03::sum);
202+
executorService.submit(futureTask);
203+
Integer result = null;
204+
try {
205+
result = futureTask.get();
206+
} catch (InterruptedException | ExecutionException e) {
207+
e.printStackTrace();
208+
}
209+
executorService.shutdown();
210+
return result;
211+
};
212+
213+
public static final Supplier<Integer> byCompletableFuture = () -> {
214+
CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(Homework03::sum);
215+
return completableFuture.join();
216+
};
217+
}

04fx/homework1003/pom.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<groupId>len.xiyuan.spring.boot</groupId>
13-
<artifactId>len-xiyuan-spring-boot-starter</artifactId>
13+
<artifactId>test-starter</artifactId>
1414
<version>0.0.1-SNAPSHOT</version>
1515
<packaging>jar</packaging>
1616
<description>Demo project for Spring Boot Starter</description>
@@ -28,13 +28,15 @@
2828
<groupId>org.springframework.boot</groupId>
2929
<artifactId>spring-boot-configuration-processor</artifactId>
3030
</dependency>
31-
<dependency>
32-
<groupId>io.kimmking</groupId>
33-
<artifactId>springboot01</artifactId>
34-
<version>0.0.1-SNAPSHOT</version>
35-
<scope>compile</scope>
36-
</dependency>
3731

3832
</dependencies>
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-maven-plugin</artifactId>
38+
</plugin>
39+
</plugins>
40+
</build>
3941

4042
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"hints": [],
3+
"groups": [
4+
{
5+
"sourceType": "pojo.Student",
6+
"name": "diy.stu",
7+
"type": "pojo.Student"
8+
}
9+
],
10+
"properties": [
11+
{
12+
"sourceType": "pojo.Student",
13+
"defaultValue": 0,
14+
"name": "diy.stu.id",
15+
"type": "java.lang.Integer"
16+
},
17+
{
18+
"sourceType": "pojo.Student",
19+
"name": "diy.stu.name",
20+
"type": "java.lang.String"
21+
}
22+
]
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=config.StuConfigration
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#Created by Apache Maven 3.6.3
2+
version=0.0.1-SNAPSHOT
3+
groupId=len.xiyuan.spring.boot
4+
artifactId=test-starter
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
META-INF\spring-configuration-metadata.json
2+
pojo\Student.class
3+
config\StuConfigration.class
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
D:\workspace\IdeaProject\JavaCourseCodes\04fx\homework1003\src\main\java\config\StuConfigration.java
2+
D:\workspace\IdeaProject\JavaCourseCodes\04fx\homework1003\src\main\java\pojo\Student.java

04fx/homework1003/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst

Whitespace-only changes.
Binary file not shown.

0 commit comments

Comments
 (0)