diff --git a/Chapter4-1-2/pom.xml b/Chapter4-1-2/pom.xml new file mode 100644 index 0000000..b25e68b --- /dev/null +++ b/Chapter4-1-2/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.didispace + Chapter4-1-2 + 1.0.0 + jar + + Chapter4-1-2 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter4-1-2/src/main/java/com/didispace/Application.java b/Chapter4-1-2/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..aa4d3f1 --- /dev/null +++ b/Chapter4-1-2/src/main/java/com/didispace/Application.java @@ -0,0 +1,16 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication +@EnableAsync +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter4-1-2/src/main/java/com/didispace/async/Task.java b/Chapter4-1-2/src/main/java/com/didispace/async/Task.java new file mode 100644 index 0000000..d095878 --- /dev/null +++ b/Chapter4-1-2/src/main/java/com/didispace/async/Task.java @@ -0,0 +1,51 @@ +package com.didispace.async; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncResult; +import org.springframework.stereotype.Component; + +import java.util.Random; +import java.util.concurrent.Future; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/16 下午12:58. + * @blog http://blog.didispace.com + */ +@Component +public class Task { + + public static Random random =new Random(); + + @Async + public Future doTaskOne() throws Exception { + System.out.println("开始做任务一"); + long start = System.currentTimeMillis(); + Thread.sleep(random.nextInt(10000)); + long end = System.currentTimeMillis(); + System.out.println("完成任务一,耗时:" + (end - start) + "毫秒"); + return new AsyncResult<>("任务一完成"); + } + + @Async + public Future doTaskTwo() throws Exception { + System.out.println("开始做任务二"); + long start = System.currentTimeMillis(); + Thread.sleep(random.nextInt(10000)); + long end = System.currentTimeMillis(); + System.out.println("完成任务二,耗时:" + (end - start) + "毫秒"); + return new AsyncResult<>("任务二完成"); + } + + @Async + public Future doTaskThree() throws Exception { + System.out.println("开始做任务三"); + long start = System.currentTimeMillis(); + Thread.sleep(random.nextInt(10000)); + long end = System.currentTimeMillis(); + System.out.println("完成任务三,耗时:" + (end - start) + "毫秒"); + return new AsyncResult<>("任务三完成"); + } + +} diff --git a/Chapter4-1-2/src/main/resources/application.properties b/Chapter4-1-2/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/Chapter4-1-2/src/test/java/com/didispace/ApplicationTests.java b/Chapter4-1-2/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..d6f26ef --- /dev/null +++ b/Chapter4-1-2/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,43 @@ +package com.didispace; + +import com.didispace.async.Task; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.concurrent.Future; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class ApplicationTests { + + @Autowired + private Task task; + + @Test + public void test() throws Exception { + + long start = System.currentTimeMillis(); + + Future task1 = task.doTaskOne(); + Future task2 = task.doTaskTwo(); + Future task3 = task.doTaskThree(); + + while(true) { + if(task1.isDone() && task2.isDone() && task3.isDone()) { + // 三个任务都调用完成,退出循环等待 + break; + } + Thread.sleep(1000); + } + + long end = System.currentTimeMillis(); + + System.out.println("任务全部完成,总耗时:" + (end - start) + "毫秒"); + + } + +}