java 多线程跑定时安全性_详解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();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值