SpringBoot线程池与创建线程方式

SpringBoot线程池与创建线程方式

这里介绍两种:

  • ①注解式:

package com.config.async;

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;

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        //线程池设置
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize( 8 );//核心线程
        taskExecutor.setMaxPoolSize( 16 );//最大线程
        taskExecutor.setQueueCapacity( 40 );//队列大小
        taskExecutor.setThreadNamePrefix( "async-service-" );
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        //直接返回 祖籍 AsyncUncaughtExceptionHandler 对象
        return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();
    }
}
  • 直接注解创建线程
private final static Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);
    @Async
    public void asyncTask1 (int i){
        logger.info( "asyncTask1  异步执行  === > 第 {} 次 ",i );
    }

    @Async
    public void asyncTask2 (int i){
        logger.info( "asyncTask2  异步执行  === > 第 {} 次",i );
    }
  • 测试代码:
@Test
public void AsyncTest (){
	for (int i = 0; i < 11; i++) {
    	this.asyncService.asyncTask1( i );
		this.asyncService.asyncTask2( i );
			
	}
}

 

  • ②配置线程调度管理器

package com.config.async;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 异步线程调度管理器 by CHENYB of date 2019-07-25
 */
public class CustomAsyncScheduler {

    private volatile static CustomAsyncScheduler instance;
    private static ThreadPoolExecutor chnlBackendQueryPool;

    private CustomAsyncScheduler () { }

    @SuppressWarnings( {"rawtypes","static-access","unchecked"} )
    public static CustomAsyncScheduler getInstance() {
        if (instance == null){
            synchronized (CustomAsyncScheduler.class){
                if (instance == null ) {
                    instance = new CustomAsyncScheduler();
                    BlockingQueue queue = new LinkedBlockingQueue();
                    chnlBackendQueryPool = new ThreadPoolExecutor( 50, 100, 30, TimeUnit.SECONDS, queue );
                    chnlBackendQueryPool.allowCoreThreadTimeOut( true );
                    instance.setChnlBackendQueryPool( chnlBackendQueryPool );
                }
            }
        }
        return instance;
    }

    public ThreadPoolExecutor getChnlBackendQueryPool() {
        return chnlBackendQueryPool;
    }

    public static void setChnlBackendQueryPool(ThreadPoolExecutor chnlBackendQueryPool) {
        CustomAsyncScheduler.chnlBackendQueryPool = chnlBackendQueryPool;
    }

    public static void setInstance(CustomAsyncScheduler instance) {
        CustomAsyncScheduler.instance = instance;
    }
}
  •  创建方式:
public void executeAsuncTask3(int i){
        CustomAsyncScheduler.getInstance().getChnlBackendQueryPool().execute( new Runnable() {
            @Override
            public void run() {
                logger.info( "asyncTask3  异步执行  === > 第 {} 次",i );

            }
        } );
    }

    public void executeAsuncTask4(int i){
        CustomAsyncScheduler.getInstance().getChnlBackendQueryPool().execute( new Runnable() {
            @Override
            public void run() {
                logger.info( "asyncTask4  异步执行  === > 第 {} 次",i );
            }
        } );
    }
  • 测试代码:
@Test
public void AsyncTest (){
	for (int i = 0; i < 11; i++) {
		this.asyncService.executeAsuncTask3( i );
		this.asyncService.executeAsuncTask4( i );
	}
}

 

 chenyb随笔记录,方便自己学习

2019-07-25

 

你可能感兴趣的:(SpringBoot,Spring,Cloud,Utils)