定義一個(gè)job:ranJob,設(shè)置每秒執(zhí)行一次,設(shè)置不允許覆蓋并發(fā)執(zhí)行
- <bean id="rankJob" class="com.chinacache.www.logstat.job.RankJob" />
- <bean id="rankJobDetail"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject" ref="rankJob" />
- <property name="targetMethod" value="execute" />
- <property name="concurrent" value="<span style="color: #ff0000;"><strong>false</strong></span>" />
- </bean>
- <bean id="rankJobTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
- <property name="jobDetail" ref="rankJobDetail" />
- <!-- 單位 ms,半小時(shí) 1800000 ms -->
- <property name="repeatInterval" value="<span style="color: #ff0000;"><strong>1000</strong></span>" />
- </bean>
job代碼:
- System.out.println("Start job");
- ExecutorService exec = Executors.newFixedThreadPool(1);
-
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- System.out.println("thread start");
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("thread end");
- }
- });
- exec.execute(thread);
- System.out.println("end job");
程序輸出結(jié)果:
- Start job
- end job
- <span style="color: #ff0000;"><strong>thread start</strong></span>
- Start job
- end job
- thread start
- Start job
- end job
- thread start
- Start job
- end job
- thread start
- <strong><span style="color: #ff0000;">thread end</span></strong>
從結(jié)果可以看到,job的并發(fā)覆蓋配置似乎根本沒(méi)有生效,原因是:job沒(méi)有關(guān)注多線程執(zhí)行情況
修改job代碼,添加如下代碼在job訪問(wèn)最后,線程處理完job才結(jié)束,
- while (!exec.isTerminated()) {
- // 等待所有子線程結(jié)束,才退出主線程
- }
修改代碼后程序結(jié)果:
- Start job
- thread start
- thread end
可以看到j(luò)ob始終沒(méi)有結(jié)束,說(shuō)明ExecutorService始終沒(méi)有終止,看看文檔,加入shutdonw()方法,job所有代碼如下:
- public void execute() throws InterruptedException {
- System.out.println("Start job");
- ExecutorService exec = Executors.newFixedThreadPool(1);
-
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- System.out.println("thread start");
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("thread end");
- }
- });
- exec.execute(thread);
- exec.shutdown();
- while (!exec.isTerminated()) {
- // 等待所有子線程結(jié)束,才退出主線程
- }
- System.out.println("end job");
- }
打印結(jié)果如下:
- Start job
- thread start
- thread end
- end job
-
- Start job
- thread start
- thread end
- end job
-
- Start job
- thread start
- thread end
- end job
OK,至此spring quartz多線程并發(fā)問(wèn)題解決。回顧下,我們要使用isTerminated()方法等多線程結(jié)束后在結(jié)束job;多線程任務(wù)派發(fā)結(jié)束后,要使用shutdown()方法順序關(guān)閉線程(等待正在執(zhí)行任務(wù),不接受新任務(wù))