Test的原因吗?还是Tomcat的原因?
1.单元测试
@Test
public void test(){
System.out.println(Thread.currentThread().getName());
new Thread(() -> {
{
try {
System.out.println(Thread.currentThread().getName());
Thread.currentThread().sleep(1000);
System.out.println(Thread.currentThread().getName() + "over");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
System.out.println(Thread.currentThread().getName() + ":over");
}
结果:
main
main:over
Thread-0
2.SpringBootTest
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootRedisApplicationTests {
@Test
void test() throws InterruptedException {
System.out.println(Thread.currentThread().getName());
new Thread(() -> {
{
try {
System.out.println(Thread.currentThread().getName());
Thread.currentThread().sleep(1000);
System.out.println(Thread.currentThread().getName() + "over");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
System.out.println(Thread.currentThread().getName() + ":over");
}
结果:
main
main:over
Thread-3
3.普通Java项目
public static void main(String[] args) throws Exception {
System.out.println(Thread.currentThread().getName());
new Thread(() -> {
{
try {
System.out.println(Thread.currentThread().getName());
Thread.currentThread().sleep(1000);
System.out.println(Thread.currentThread().getName() + "over");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
System.out.println(Thread.currentThread().getName() + ":over");
}
结果:
main
main:over
Thread-0
Thread-0over
4.Springboot项目
@SpringBootApplication
public class SpringbootRedisApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringbootRedisApplication.class, args);
System.out.println(Thread.currentThread().getName());
new Thread(() -> {
{
try {
System.out.println(Thread.currentThread().getName());
Thread.currentThread().sleep(1000);
System.out.println(Thread.currentThread().getName() + "over");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
System.out.println(Thread.currentThread().getName() + ":over");
}
}
结果:
restartedMain
restartedMain:over
Thread-9
Thread-9over