资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

深入浅析spring中的多线程与定时任务

深入浅析spring中的多线程与定时任务?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!创新互联建站为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,成都网站建设、成都网站设计负责任的成都网站制作公司!

1.spring多线程任务的使用

spring通过任务执行器TaskExecutor来实现多线程与并发编程。通常使用ThreadPoolTaskExecutor来实现一个基于线程池的TaskExecutor.

首先你要实现AsyncConfigurer 这个接口,目的是开启一个线程池

代码如下:

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;

/**
 * 注入一个线程池
 * @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;
  }

  
}

然后注入一个类,实现你的业务,并在你的Bean的方法中使用@Async注解来声明其是一个异步任务

代码如下:

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

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

/**
 * 线程池任务
 * @author mingge
 *
 */
@Service
public class TaskService {

  @Async
  public void executeAsyncTask(int i){
    System.out.println("执行异步任务:"+i);
  }
  
  @Async
  public void executeAsyncTask1(int i){
    System.out.println("执行异步任务1:"+(i+i));
  }
}

最后通过测试,可以看到你的实现是异步执行了.

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);
    }
    //最后可以根据结果可以看出结果是并发执行而不是顺序执行的呢
    context.close();
  }
}

2.spring定时任务的使用

在java原生态中,我们使用timer就可以了,这里小编说一些在Spring中的定时任务的使用

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//开启对定时器的支持
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("我是定时任务:"+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();
  }
}

关于深入浅析spring中的多线程与定时任务问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站题目:深入浅析spring中的多线程与定时任务
分享URL:http://cdkjz.cn/article/ggsech.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220