跑线程为什么只打印一条信息
代码如下
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.thread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
public class MyThreadTest {
public void first(){
log.info("first");
}
public void second(){
log.info("second");
}
}
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(){
MyThreadTest myThreadTest = new MyThreadTest();
simpleExecutor.submit(()->{
log.info("进入simpleThreadTest");
try {
Thread.sleep(1000);
myThreadTest.first();
}catch (Exception e){
log.error("sleep error");
}
myThreadTest.second();
});
}
}
结果如下
问题:为什么不打印second?