Skip to content

Commit 7b10dc2

Browse files
committed
sunnychan
1 parent c3ae83a commit 7b10dc2

File tree

16 files changed

+223
-0
lines changed

16 files changed

+223
-0
lines changed

.idea/compiler.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
<module>spring-data</module>
4646
<module>spring-bean</module>
4747
<module>spring-cookbook</module>
48+
<module>spring-task</module>
4849
</modules>
4950
</project>

spring-cookbook/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Spring Cookbook
2+
3+
1. 获取配置信息
4+
5+
2. 异步
6+

spring-cookbook/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
<groupId>org.springframework.boot</groupId>
3737
<artifactId>spring-boot-starter-web</artifactId>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
</dependency>
43+
3944
<dependency>
4045
<groupId>com.alibaba</groupId>
4146
<artifactId>fastjson</artifactId>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.sunny.demo.spring.cookbook.async;
2+
3+
import java.util.concurrent.Future;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.springframework.scheduling.annotation.Async;
7+
import org.springframework.scheduling.annotation.AsyncResult;
8+
import org.springframework.stereotype.Component;
9+
10+
/**
11+
12+
* 测试异步执行
13+
*/
14+
15+
@Component
16+
public class AsyncTask {
17+
18+
/**
19+
* @param second sleep time
20+
*/
21+
@Async
22+
public Future<String> doTask1(Integer second) {
23+
try {
24+
System.out.println("Async do task 1 start : " + System.currentTimeMillis() / 1000);
25+
TimeUnit.SECONDS.sleep(second);
26+
System.out.println("Async do task 1 end : " + System.currentTimeMillis() / 1000);
27+
} catch (InterruptedException e ) {
28+
System.out.println("InterruptedException occur");
29+
}
30+
return new AsyncResult<>("task 1 return");
31+
}
32+
33+
/**
34+
* @param second sleep time
35+
*/
36+
@Async
37+
public Future<String> doTask2(Integer second) {
38+
try {
39+
System.out.println("Async do task 2 start : " + System.currentTimeMillis() / 1000);
40+
TimeUnit.SECONDS.sleep(second);
41+
System.out.println("Async do task 2 end : " + System.currentTimeMillis() / 1000);
42+
} catch (InterruptedException e ) {
43+
System.out.println("InterruptedException occur");
44+
}
45+
return new AsyncResult<>("task 2 return");
46+
}
47+
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.sunny.demo.spring.cookbook.async;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
9+
*/
10+
11+
@Component
12+
public class SyncTask {
13+
14+
public void doTask1() {
15+
try {
16+
System.out.println("do task 1 start : " + System.currentTimeMillis());
17+
TimeUnit.SECONDS.sleep(1);
18+
System.out.println("do task 1 end : " + System.currentTimeMillis());
19+
} catch (InterruptedException e ) {
20+
System.out.println("InterruptedException occur");
21+
}
22+
23+
}
24+
25+
public void doTask2() {
26+
try {
27+
System.out.println("do task 2 start : " + System.currentTimeMillis());
28+
TimeUnit.SECONDS.sleep(1);
29+
System.out.println("do task 2 end : " + System.currentTimeMillis());
30+
} catch (InterruptedException e ) {
31+
System.out.println("InterruptedException occur");
32+
}
33+
34+
}
35+
36+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* 对比同步与异步
3+
*/
4+
package me.sunny.demo.spring.cookbook.async;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package me.sunny.demo.spring.cookbook.async;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableAsync;
6+
7+
@SpringBootApplication
8+
@EnableAsync
9+
public class SpringApplicationForTest {
10+
11+
public static void main(String[] args){
12+
SpringApplication.run(SpringApplicationForTest.class);
13+
}
14+
15+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package me.sunny.demo.spring.cookbook.async;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Future;
5+
6+
import me.sunny.demo.spring.cookbook.async.AsyncTask;
7+
import me.sunny.demo.spring.cookbook.async.SyncTask;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
14+
@RunWith(SpringRunner.class)
15+
@SpringBootTest
16+
public class TestAsync {
17+
18+
@Autowired
19+
SyncTask syncTask;
20+
21+
@Autowired
22+
AsyncTask asyncTask;
23+
24+
@Test
25+
public void testSyncTask() {
26+
syncTask.doTask1();
27+
syncTask.doTask2();
28+
}
29+
30+
/**
31+
* task 1 先完成
32+
* future1.get() 和 future2.get() 都阻塞
33+
*/
34+
@Test
35+
public void testAsyncTask1() throws InterruptedException, ExecutionException {
36+
Future<String> future1 = asyncTask.doTask1(1);
37+
Future<String> future2 = asyncTask.doTask2(2);
38+
String result1 = future1.get();
39+
System.out.println(result1);
40+
String result2 = future2.get();
41+
System.out.println(result2);
42+
}
43+
44+
/**
45+
* task 2 先完成
46+
* future1.get() 会阻塞,future2.get()不会阻塞,因为 task 2 执行快,
47+
* 在等待 task 1完成的时候,task 2已经执行完成。
48+
*/
49+
@Test
50+
public void testAsyncTask2() throws InterruptedException, ExecutionException {
51+
Future<String> future1 = asyncTask.doTask1(2);
52+
Future<String> future2 = asyncTask.doTask2(1);
53+
String result1 = future1.get();
54+
System.out.println(result1);
55+
String result2 = future2.get();
56+
System.out.println(result2);
57+
}
58+
59+
60+
}

0 commit comments

Comments
 (0)