资讯专栏INFORMATION COLUMN

Springboot对多线程的支持详解

adie / 2086人阅读

摘要:对多线程的支持详解这两天看阿里的开发手册,到多线程的时候说永远不要用这种方式来使用多线程。在使用线程池的大多数情况下都是异步非阻塞的。二配置类配置类代码如下下午解读利用来开启对于异步任务的支持配置类实现接口,返回一个线程池对象。

Springboot对多线程的支持详解
这两天看阿里的JAVA开发手册,到多线程的时候说永远不要用 new Thread()这种方式来使用多线程。确实是这样的,我一直在用线程池,到了springboot才发现他已经给我们提供了很方便的线程池机制。
本博客代码托管在github上https://github.com/gxz0422042...
一、介绍

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76485.html

相关文章

  • Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)

    摘要:线程切换效率低下单机核数固定,线程爆炸之后操作系统频繁进行线程切换,应用性能急剧下降。线程切换效率低下由于模型中线程数量大大降低,线程切换效率因此也大幅度提高。将两个线程优雅地关闭。创建管道的子处理器,用于处理。 Netty+SpringBoot+FastDFS+Html5实现聊天App,项目介绍:https://segmentfault.com/a/11... Netty+Sprin...

    妤锋シ 评论0 收藏0
  • Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)

    摘要:线程切换效率低下单机核数固定,线程爆炸之后操作系统频繁进行线程切换,应用性能急剧下降。线程切换效率低下由于模型中线程数量大大降低,线程切换效率因此也大幅度提高。将两个线程优雅地关闭。创建管道的子处理器,用于处理。 Netty+SpringBoot+FastDFS+Html5实现聊天App,项目介绍:https://segmentfault.com/a/11... Netty+Sprin...

    terasum 评论0 收藏0
  • Netty+SpringBoot+FastDFS+Html5实现聊天App详解(一)

    摘要:线程切换效率低下单机核数固定,线程爆炸之后操作系统频繁进行线程切换,应用性能急剧下降。线程切换效率低下由于模型中线程数量大大降低,线程切换效率因此也大幅度提高。将两个线程优雅地关闭。创建管道的子处理器,用于处理。 Netty+SpringBoot+FastDFS+Html5实现聊天App,项目介绍:https://segmentfault.com/a/11... Netty+Sprin...

    CNZPH 评论0 收藏0
  • java篇

    摘要:多线程编程这篇文章分析了多线程的优缺点,如何创建多线程,分享了线程安全和线程通信线程池等等一些知识。 中间件技术入门教程 中间件技术入门教程,本博客介绍了 ESB、MQ、JMS 的一些知识... SpringBoot 多数据源 SpringBoot 使用主从数据源 简易的后台管理权限设计 从零开始搭建自己权限管理框架 Docker 多步构建更小的 Java 镜像 Docker Jav...

    honhon 评论0 收藏0
  • 写这么多系列博客,怪不得找不到女朋友

    摘要:前提好几周没更新博客了,对不断支持我博客的童鞋们说声抱歉了。熟悉我的人都知道我写博客的时间比较早,而且坚持的时间也比较久,一直到现在也是一直保持着更新状态。 showImg(https://segmentfault.com/img/remote/1460000014076586?w=1920&h=1080); 前提 好几周没更新博客了,对不断支持我博客的童鞋们说声:抱歉了!。自己这段时...

    JerryWangSAP 评论0 收藏0

发表评论

0条评论

adie

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<