Skip to content

Commit 9e45863

Browse files
committed
Signed-off-by: chx <chxpostbox@outlook.com>
1 parent 5978331 commit 9e45863

File tree

12 files changed

+349
-118
lines changed

12 files changed

+349
-118
lines changed

springBoot/.idea/workspace.xml

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

springBoot/springBoot.iml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="Spring" name="Spring">
5+
<configuration>
6+
<fileset id="fileset" name="Spring Application Context" removed="false">
7+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p2_4_2Profile/ProfileConfig.java</file>
8+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p2_1_1Scope/ScopeConfig.java</file>
9+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p3/p2_TaskExecutor/TaskExecutorConfig.java</file>
10+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p1_3_1/DiConfig.java</file>
11+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p2_3_2BeanLifecycle/PrePostConfig.java</file>
12+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p3/p1_SpringAware/AwareConfig.java</file>
13+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p2_2_2SpringEL/ElConfig.java</file>
14+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p1_3_2/JavaConfig.java</file>
15+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p2_5_2ApplicationEvent/EventConfig.java</file>
16+
<file>file://$MODULE_DIR$/src/main/java/cn/hncu/p1_3_3_aop/AopConfig.java</file>
17+
</fileset>
18+
</configuration>
19+
</facet>
20+
</component>
321
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
422
<output url="file://$MODULE_DIR$/target/classes" />
523
<output-test url="file://$MODULE_DIR$/target/test-classes" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.hncu.p3.p3_taskscheduler;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: 陈浩翔.
8+
* Date: 2016/11/22.
9+
* Time: 下午 10:34.
10+
* Explain:运行类
11+
*/
12+
public class Main {
13+
public static void main(String[] args) {
14+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskScheduleConfig.class);
15+
}
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.hncu.p3.p3_taskscheduler;
2+
3+
import org.springframework.scheduling.annotation.Scheduled;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.text.SimpleDateFormat;
7+
import java.util.Date;
8+
9+
/**
10+
* Created with IntelliJ IDEA.
11+
* User: 陈浩翔.
12+
* Date: 2016/11/22.
13+
* Time: 下午 10:25.
14+
* Explain:计划任务执行类
15+
*/
16+
@Service
17+
public class ScheduledTaskService {
18+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
19+
20+
@Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
21+
public void reportCurrentTime(){
22+
System.out.println("每隔5秒执行一次 "+dateFormat.format(new Date()));
23+
}
24+
25+
@Scheduled(cron = "0 38 22 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天22点38分执行;
26+
//cron是UNIX和类UNIX(Linux)系统下的定时任务
27+
public void fixTimeExecution(){
28+
System.out.println("在指定时间 "+dateFormat.format(new Date())+" 执行");
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.hncu.p3.p3_taskscheduler;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
/**
8+
* Created with IntelliJ IDEA.
9+
* User: 陈浩翔.
10+
* Date: 2016/11/22.
11+
* Time: 下午 10:32.
12+
* Explain:配置类
13+
*/
14+
15+
@Configuration
16+
@ComponentScan("cn.hncu.p3.p3_taskscheduler")
17+
@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
18+
public class TaskScheduleConfig {
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.hncu.p3.p4_conditional;
2+
3+
import java.io.*;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: 陈浩翔.
8+
* Date: 2016/11/22.
9+
* Time: 下午 11:01.
10+
* Explain:
11+
*/
12+
public class AA {
13+
public static void main(String[] args) {
14+
try {
15+
DataOutputStream dout=new DataOutputStream(new FileOutputStream("d:/c.txt"));
16+
17+
short i=0;
18+
short j=1;
19+
do{
20+
dout.writeShort(i);
21+
dout.writeShort(j);
22+
23+
i=(short) (i+j);
24+
j=(short) (i+j);
25+
}while(i>0);
26+
27+
} catch (FileNotFoundException e) {
28+
e.printStackTrace();
29+
}catch(IOException e1){
30+
31+
}
32+
}
33+
34+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#分析
2+
3+
在Spring中,通过任务执行器,也就是TaskExecutor来实现多线程和并发编程。
4+
5+
使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor。
6+
而实际开发中任务一般是非阻碍的,也就是非异步的,所以我们要在配置类中通过@EnableAsync开启对异步任务的支持,并通过在实际执行的Bean的方法中使用@Async注解来声明其是一个异步任务。
7+
8+
9+
进行本示例的演示,需要先配置好Maven和Spring哦、
10+
见:
11+
<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a>
12+
13+
#示例
14+
15+
##配置类
16+
17+
首先看一下配置类。
18+
现在全部使用Java配置哦,不用xml了。
19+
20+
```
21+
package cn.hncu.p3.p2_TaskExecutor;
22+
23+
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
24+
import org.springframework.context.annotation.ComponentScan;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.scheduling.annotation.AsyncConfigurer;
27+
import org.springframework.scheduling.annotation.EnableAsync;
28+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
29+
30+
import java.util.concurrent.Executor;
31+
32+
/**
33+
* Created with IntelliJ IDEA.
34+
* User: 陈浩翔.
35+
* Date: 2016/11/18.
36+
* Time: 上午 9:35.
37+
* Explain:配置类
38+
*/
39+
@Configuration
40+
@ComponentScan("cn.hncu.p3.p2_TaskExecutor")
41+
@EnableAsync //利用@EnableAsync注解开启异步任务支持
42+
public class TaskExecutorConfig implements AsyncConfigurer{
43+
//配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法,并返回一个ThreadPoolTaskExevutor
44+
//这样我们就获得了一个基于线程池的TaskExecutor
45+
@Override
46+
public Executor getAsyncExecutor() {
47+
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
48+
taskExecutor.setCorePoolSize(5);//线程池维护线程的最少数量
49+
taskExecutor.setMaxPoolSize(10);//线程池维护线程的最大数量
50+
taskExecutor.setQueueCapacity(25);//线程池所使用的缓冲队列
51+
taskExecutor.initialize();
52+
return taskExecutor;
53+
}
54+
55+
@Override
56+
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
57+
return null;
58+
}
59+
}
60+
61+
```
62+
63+
64+
##任务执行类
65+
66+
也就是实际运行的,需要异步执行的类
67+
68+
```
69+
package cn.hncu.p3.p2_TaskExecutor;
70+
71+
import org.springframework.scheduling.annotation.Async;
72+
import org.springframework.stereotype.Service;
73+
74+
/**
75+
* Created with IntelliJ IDEA.
76+
* User: 陈浩翔.
77+
* Date: 2016/11/18.
78+
* Time: 上午 10:57.
79+
* Explain:任务执行类
80+
*/
81+
@Service
82+
public class AsyncTaskService {
83+
84+
@Async
85+
//通过@Async注解表明该方法是个异步方法,如果注解在类级别,则表明该类所有的方法都是异步方法。
86+
// 而这里的方法自动被注入使用ThreadPoolTaskExecutor作为TaskExecutor
87+
public void executeAsyncTask(Integer i){
88+
System.out.println("executeAsyncTask:"+i);
89+
}
90+
91+
@Async
92+
public void executeAsyncTaskPlus(Integer i){
93+
System.out.println("executeAsyncTaskPlus:"+i);
94+
}
95+
96+
}
97+
98+
```
99+
100+
##运行类
101+
102+
为了测试而写的运行调用方法的类
103+
104+
```
105+
package cn.hncu.p3.p2_TaskExecutor;
106+
107+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
108+
109+
/**
110+
* Created with IntelliJ IDEA.
111+
* User: 陈浩翔.
112+
* Date: 2016/11/18.
113+
* Time: 上午 11:04.
114+
* Explain:运行类
115+
*/
116+
public class Main {
117+
public static void main(String[] args) {
118+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
119+
120+
AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
121+
for(int i=0;i<10;i++){
122+
asyncTaskService.executeAsyncTaskPlus(i);
123+
asyncTaskService.executeAsyncTask(i+1);
124+
}
125+
context.close();
126+
127+
}
128+
129+
}
130+
131+
```
132+
133+
#运行结果
134+
![](http://img.blog.csdn.net/20161118113251428)
135+
136+
运行结果长了一点,所以只传了这么一点。
137+
但是已经能够看出了,假如是原来那样的,是异步执行,那么肯定偶数行的输出比前一个奇数行的输出是大1的。
138+
139+
结果不是那样,它们不是异步进行的,在这里由一个主线程(main线程)。
140+
for循环里面,每运行一行调用方法的,就会开一个线程。
141+
也就是说,你每次的运行结果可能会不一样!
142+
所以,如果你的运行结果和我的不一样,不要慌哦。
717 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)