在springBoot中使用java线程池ExecutorService

1、利用IOC模式引入线程池的类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 数据收集配置,主要作用在于Spring启动时自动加载一个ExecutorService对象.
 * @author tao
 * @date 2018/5/14
 * 
 * update by Cliff at 2027/11/03
 */
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ExecutorService getThreadPool(){
        return Executors.newFixedThreadPool(20);
    }
}

2、注入 ExecutorService 直接使用

@Autowired
    private ExecutorService executorService;

public void test(){
        executorService.execute(new Runnable() {
            public void run() {
                System.out.println("Asynchronous task");
            }
        });
    }

你可能感兴趣的:(java)