网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Springboot如何实现多线程支持

这篇文章给大家分享的是有关Springboot如何实现多线程支持的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!创新互联公司为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,成都网站制作、成都做网站负责任的成都网站制作公司!

一、介绍

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如何实现多线程支持”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


分享标题:Springboot如何实现多线程支持
文章路径:http://bjjierui.cn/article/peiosi.html

其他资讯