最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解spring多線程與定時(shí)任務(wù)

 更新時(shí)間:2017年04月11日 10:14:37   作者:全力以赴001  
本篇文章主要介紹了spring多線程與定時(shí)任務(wù),詳細(xì)的介紹了spring多線程任務(wù)和spring定時(shí)任務(wù),有興趣的可以了解一下。

本篇主要描述一下spring的多線程的使用與定時(shí)任務(wù)的使用.

1.spring多線程任務(wù)的使用

spring通過任務(wù)執(zhí)行器TaskExecutor來實(shí)現(xiàn)多線程與并發(fā)編程。通常使用ThreadPoolTaskExecutor來實(shí)現(xiàn)一個(gè)基于線程池的TaskExecutor.

首先你要實(shí)現(xiàn)AsyncConfigurer 這個(gè)接口,目的是開啟一個(gè)線程池

代碼如下:

package com.foreveross.service.weixin.test.thread;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 注入一個(gè)線程池
 * @author mingge
 *
 */

@Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {

  @Override
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(20);
    taskExecutor.setQueueCapacity(25);
    taskExecutor.initialize();
    return taskExecutor;
  }

  @Override
  public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    return null;
  }

  
}

然后注入一個(gè)類,實(shí)現(xiàn)你的業(yè)務(wù),并在你的Bean的方法中使用@Async注解來聲明其是一個(gè)異步任務(wù)

代碼如下:

package com.foreveross.service.weixin.test.thread;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 線程池任務(wù)
 * @author mingge
 *
 */
@Service
public class TaskService {

  @Async
  public void executeAsyncTask(int i){
    System.out.println("執(zhí)行異步任務(wù):"+i);
  }
  
  @Async
  public void executeAsyncTask1(int i){
    System.out.println("執(zhí)行異步任務(wù)1:"+(i+i));
  }
}

最后通過測(cè)試,可以看到你的實(shí)現(xiàn)是異步執(zhí)行了.

package com.foreveross.service.weixin.test.thread;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


/**
 * 
 * @author mingge
 *
 */
public class Test {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
    TaskService taskService=context.getBean(TaskService.class);
    for(int i=0;i<20;i++){
      taskService.executeAsyncTask(i);
      taskService.executeAsyncTask1(i);
    }
    //最后可以根據(jù)結(jié)果可以看出結(jié)果是并發(fā)執(zhí)行而不是順序執(zhí)行的呢
    context.close();
  }
}

2.spring定時(shí)任務(wù)的使用

在java原生態(tài)中,我們使用timer就可以了,這里小編說一些在Spring中的定時(shí)任務(wù)的使用

package com.foreveross.service.weixin.test.thread;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableScheduling//開啟對(duì)定時(shí)器的支持
public class TaskSchedulerConfig {

}
package com.foreveross.service.weixin.test.thread;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class TimerTaskJob {

  @Scheduled(fixedRate=2000)
  public void test(){
    System.out.println("我是定時(shí)任務(wù):"+new Date().getSeconds());
  }
}
package com.foreveross.service.weixin.test.thread;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestTimer {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
    
    //context.close();
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

揭阳市| 金湖县| 鄂托克前旗| 平利县| 峨边| 浦城县| 延长县| 句容市| 永州市| 甘德县| 睢宁县| 韶关市| 石首市| 广昌县| 东莞市| 神农架林区| 柳林县| 观塘区| 苍溪县| 榆中县| 临洮县| 日喀则市| 都兰县| 渑池县| 定襄县| 威海市| 宁武县| 广宁县| 江源县| 泾源县| 洮南市| 广安市| 赞皇县| 伊春市| 永川市| 仪征市| 三穗县| 石柱| 万荣县| 万载县| 宜阳县|