好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解Springboot对多线程的支持

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

本博客代码托管在github上 https://github.com/gxz0422042 ...

一、介绍

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

二、配置类

配置类代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

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线程池对象。

三、任务执行

任务执行代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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注解表明该方法是异步方法,如果注解在类上,那表明这个类里面的所有方法都是异步的。

四、测试代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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对多线程的支持,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://segmentfault.com/a/1190000015766938

查看更多关于详解Springboot对多线程的支持的详细内容...

  阅读:42次