使用线程池跑线程,当第一步进行完后暂停一秒,然后去跑第二步,没有打印第二部信息
线程池配置如下:
package com.test.pool.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
public class ThreadConfig {
@Qualifier("simpleExecutor")
@Bean
public ThreadPoolExecutor simpleExecutor() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(16,
16,
10,
TimeUnit.MINUTES,
new SynchronousQueue<>(),
new CustomizableThreadFactory("simple-pool-"),
new ThreadPoolExecutor.AbortPolicy());
threadPoolExecutor.allowCoreThreadTimeOut(true);
return threadPoolExecutor;
}
}
测试类如下
package com.test.pool;
import com.test.pool.thread.MyThreadTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class SimpleThreadTest {
@Resource
ExecutorService simpleExecutor;
@Test
public void simpleThreadTest(){
simpleExecutor.submit(()->{
log.info("进入simpleThreadTest");
try {
first();
Thread.sleep(100);
}catch (Exception e){
log.error("sleep error");
}
second();
});
}
public void first(){
log.info("first");
}
public void second(){
log.info("second");
}
}
运行结果如下:
问:第二步的信息呢?0.1秒应该很快啊