搭建springboot環境
建立ThreadConfigspring
/** * 線程 * * @author zhoumin * @create 2018-09-18 13:58 */ @Configuration @EnableAsync public class ThreadConfig implements AsyncConfigurer{ @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(8); executor.setMaxPoolSize(1000); executor.setQueueCapacity(500); executor.setKeepAliveSeconds(30000); executor.initialize(); return executor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return null; } }
建立service和接口springboot
void test(int i);
service實現類異步
@Override @Async public void test(int i) { System.out.println("線程" + Thread.currentThread().getName() + " 執行異步任務:" + i); }
測試:ide
@RunWith(SpringRunner.class) @SpringBootTest public class BaseTest { }
/** * @author zhoumin * @create 2018-09-18 14:12 */ public class ThreadTest extends BaseTest{ @Autowired private DeviceStatisticsTaskService deviceStatisticsTaskService; @org.junit.Test public void threadTest() { for (int i = 0; i < 5000; i++) { deviceStatisticsTaskService.test(i); } } }