1 spring bean 指定顺序初始化 @BeforeConfigution
2 main 与 test 测试多线程问题
2.1 案例代码
package latest.practice.demo.thread;
import org.junit.jupiter.api.Test;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* thread 测试
*
* @author Bruce
* @date 2021/8/23 14:30
*/
public class ThreadDemo {
/**
* 测试守护线程
*/
@Test
public void testDaemonThread() throws InterruptedException {
Date now = new Date();
System.out.println(">>>>>>>>>> thread start..." + Thread.currentThread().getName() + "," + now);
Thread thread = new Thread(() -> {
System.out.println(">>>>>>>>>> testDaemonThread start..." + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(2L);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(">>>>> testDaemonThread finally");
}
System.out.println(">>>>>>>>>> testDaemonThread end..." + Thread.currentThread().getName());
}, "testDaemonThread");
thread.setDaemon(true);
thread.start();
// thread.join();
System.out.println(">>>>>>>>>> thread end..." + Thread.currentThread().getName()
+ " is daemon: " + Thread.currentThread().isDaemon() + "," + (System.currentTimeMillis() - now.getTime()));
/*
结果:
>>>>>>>>>> thread start...Test worker,Mon Aug 23 16:31:47 CST 2021
>>>>>>>>>> thread end...Test worker is daemon: false,3
>>>>>>>>>> testDaemonThread start...testDaemonThread
*/
}
/**
* 测试非守护线程
*/
@Test
public void testNonDaemonThread() throws InterruptedException {
Date now = new Date();
System.out.println(">>>>>>>>>> thread start..." + Thread.currentThread().getName() + "," + now);
Thread thread = new Thread(() -> {
System.out.println(">>>>>>>>>> testNonDaemonThread start..." + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(2L);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println(">>>>> testNonDaemonThread finally");
}
System.out.println(">>>>>>>>>> testNonDaemonThread end..." + Thread.currentThread().getName());
}, "testDaemonThread");
thread.setDaemon(false);
thread.start();
// thread.join();
System.out.println(">>>>>>>>>> thread end..." + Thread.currentThread().getName()
+ " is daemon: " + Thread.currentThread().isDaemon() + "," + (System.currentTimeMillis() - now.getTime()));
/*
结果:
>>>>>>>>>> thread start...Test worker,Mon Aug 23 16:32:05 CST 2021
>>>>>>>>>> thread end...Test worker is daemon: false,3
>>>>>>>>>> testNonDaemonThread start...testDaemonThread
*/
}
public static void main(String[] args) throws InterruptedException {
ThreadDemo demo = new ThreadDemo();
// demo.testNonDaemonThread();
demo.testDaemonThread();
/*
>>>>>>>>>> thread start...main,Mon Aug 23 16:33:43 CST 2021
>>>>>>>>>> thread end...main is daemon: false,41
>>>>>>>>>> testDaemonThread start...testDaemonThread
*/
}
@Test
public void test() {
}
}
2.2 案例结果
测试中发现,@Test 与 main 输出的结果不一样
testDaemonThread 方法结果:
test 的:
>>>>>>>>>> thread start...Test worker,Mon Aug 23 16:35:23 CST 2021
>>>>>>>>>> thread end...Test worker is daemon: false,2
>>>>>>>>>> testDaemonThread start...testDaemonThread
-------------------
main 的:
>>>>>>>>>> thread start...main,Mon Aug 23 16:37:04 CST 2021
>>>>>>>>>> thread end...main is daemon: false,43
>>>>>>>>>> testDaemonThread start...testDaemonThread
testNonDaemonThread 方法结果:
@Test 的
>>>>>>>>>> thread start...Test worker,Mon Aug 23 16:37:32 CST 2021
>>>>>>>>>> thread end...Test worker is daemon: false,2
>>>>>>>>>> testNonDaemonThread start...testDaemonThread
---------------
main 的:
>>>>>>>>>> thread start...main,Mon Aug 23 16:38:26 CST 2021
>>>>>>>>>> thread end...main is daemon: false,44
>>>>>>>>>> testNonDaemonThread start...testDaemonThread
>>>>> testNonDaemonThread finally
>>>>>>>>>> testNonDaemonThread end...testDaemonThread
感觉有点奇怪。