当前位置: 移动技术网 > IT编程>开发语言>Java > 详解spring多线程与定时任务

详解spring多线程与定时任务

2019年07月22日  | 移动技术网IT编程  | 我要评论
本篇主要描述一下spring的多线程的使用与定时任务的使用. 1.spring多线程任务的使用 spring通过任务执行器taskexecutor来实现多线程与并发编程

本篇主要描述一下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();
  }
}

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网