当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Springboot对多线程的支持

详解Springboot对多线程的支持

2019年07月19日  | 移动技术网IT编程  | 我要评论

迷宫物件,成人一根材,linux vi 命令

这两天看阿里的java开发手册,到多线程的时候说永远不要用 new thread()这种方式来使用多线程。确实是这样的,我一直在用线程池,到了springboot才发现他已经给我们提供了很方便的线程池机制。

本博客代码托管在github上...

一、介绍

spring是通过任务执行器(taskexecutor)来实现多线程和并发编程,使用threadpooltaskexecutor来创建一个基于线城池的taskexecutor。在使用线程池的大多数情况下都是异步非阻塞的。我们配置注解@enableasync可以开启异步任务。然后在实际执行的方法上配置注解@async上声明是异步任务。

二、配置类

配置类代码如下:

package com.spartajet.springbootlearn.thread;
import org.springframework.aop.interceptor.asyncuncaughtexceptionhandler;
import org.springframework.context.annotation.configuration;
import org.springframework.scheduling.annotation.asyncconfigurer;
import org.springframework.scheduling.annotation.enableasync;
import org.springframework.scheduling.concurrent.threadpooltaskexecutor;
import java.util.concurrent.executor;
/**
 * @description
 * @create 2017-02-22 下午11:53
 * @email gxz04220427@163.com
 */
@configuration
@enableasync
public class threadconfig implements asyncconfigurer {
  /**
   * the {@link executor} instance to be used when processing async
   * method invocations.
   */
  @override
  public executor getasyncexecutor() {
    threadpooltaskexecutor executor = new threadpooltaskexecutor();
    executor.setcorepoolsize(5);
    executor.setmaxpoolsize(15);
    executor.setqueuecapacity(25);
    executor.initialize();
    return executor;
  }
  /**
   * the {@link asyncuncaughtexceptionhandler} instance to be used
   * when an exception is thrown during an asynchronous method execution
   * with {@code void} return type.
   */
  @override
  public asyncuncaughtexceptionhandler getasyncuncaughtexceptionhandler() {
    return null;
  }
}

解读:

利用enableasync来开启springboot对于异步任务的支持

配置类实现接口asyncconfigurator,返回一个threadpooltaskexecutor线程池对象。

三、任务执行

任务执行代码:

package com.spartajet.springbootlearn.thread;
import org.springframework.scheduling.annotation.async;
import org.springframework.stereotype.service;
/**
 * @description
 * @create 2017-02-23 上午12:00
 * @email gxz04220427@163.com
 */
@service
public class asynctaskservice {
  @async
  public void executeasynctask(int i) {
    system.out.println("线程" + thread.currentthread().getname() + " 执行异步任务:" + i);
  }
}

代码解读:

通过@async注解表明该方法是异步方法,如果注解在类上,那表明这个类里面的所有方法都是异步的。

四、测试代码

package com.spartajet.springbootlearn;
import com.spartajet.springbootlearn.thread.asynctaskservice;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
@runwith (springrunner.class)
@springboottest
public class springbootlearnapplicationtests {
  @autowired
  private asynctaskservice asynctaskservice;
  @test
  public void contextloads() {
  }
  @test
  public void threadtest() {
    for (int i = 0; i < 20; i++) {
      asynctaskservice.executeasynctask(i);
    }
  }
}

测试结果:

线程threadpooltaskexecutor-4 执行异步任务:3
线程threadpooltaskexecutor-2 执行异步任务:1
线程threadpooltaskexecutor-1 执行异步任务:0
线程threadpooltaskexecutor-1 执行异步任务:7
线程threadpooltaskexecutor-1 执行异步任务:8
线程threadpooltaskexecutor-1 执行异步任务:9
线程threadpooltaskexecutor-1 执行异步任务:10
线程threadpooltaskexecutor-5 执行异步任务:4
线程threadpooltaskexecutor-3 执行异步任务:2
线程threadpooltaskexecutor-5 执行异步任务:12
线程threadpooltaskexecutor-1 执行异步任务:11
线程threadpooltaskexecutor-2 执行异步任务:6
线程threadpooltaskexecutor-4 执行异步任务:5
线程threadpooltaskexecutor-2 执行异步任务:16
线程threadpooltaskexecutor-1 执行异步任务:15
线程threadpooltaskexecutor-5 执行异步任务:14
线程threadpooltaskexecutor-3 执行异步任务:13
线程threadpooltaskexecutor-1 执行异步任务:19
线程threadpooltaskexecutor-2 执行异步任务:18
线程threadpooltaskexecutor-4 执行异步任务:17

总结

以上所述是小编给大家介绍的springboot对多线程的支持,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网