b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Springboot对多线程的支持解析

电脑杂谈  发布时间:2020-01-09 02:03:31  来源:网络整理

spring 多线程_四线程和八线程的区别_线程池与线程

这一天看阿里的JAVA开发手册spring 多线程,到多线程的之后说依然不要用 new Thread()这种方法来使用多线程。确实是这种的,我仍然在用线程池,到了springboot才看到他终于帮我们提供了很方便的线程池机制。

本博客代码托管在github上

spring 多线程_四线程和八线程的区别_线程池与线程

Spring是借助任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor来建立一个基于线城池的TaskExecutor。在使用线程池的大多数情况下都是异步非阻塞的。我们配置注解@EnableAsync可以进入异步任务。然后在实际执行的方式上配置注解@Async上声明是异步任务。

配置类代码如下:

线程池与线程_四线程和八线程的区别_spring 多线程

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;
    }
}

解读:

线程池与线程_spring 多线程_四线程和八线程的区别

利用EnableAsync来启动Springboot对于异步任务的支持配置类实现接口AsyncConfigurator,返回一个ThreadPoolTaskExecutor线程池对象。

任务执行代码:

spring 多线程_线程池与线程_四线程和八线程的区别

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注解说明该办法是异步方式spring 多线程,如果注解在类上,那说明这个类上面的所有步骤都是异步的。

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);
        }
    }
}

测试结果:


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-136269-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 刘明璐
        刘明璐

        应该不是南京判的

      • 张元一
        张元一

        现在能有6000多吨的052D相陪已经是不容易啦

      • 郑菲
        郑菲

        也不敢再找事羞辱我们

      热点图片
      拼命载入中...