1 spring bean 指定顺序初始化 @BeforeConfigution

2 main 与 test 测试多线程问题

2.1 案例代码

  1. package latest.practice.demo.thread;
  2. import org.junit.jupiter.api.Test;
  3. import java.util.Date;
  4. import java.util.concurrent.TimeUnit;
  5. /**
  6. * thread 测试
  7. *
  8. * @author Bruce
  9. * @date 2021/8/23 14:30
  10. */
  11. public class ThreadDemo {
  12. /**
  13. * 测试守护线程
  14. */
  15. @Test
  16. public void testDaemonThread() throws InterruptedException {
  17. Date now = new Date();
  18. System.out.println(">>>>>>>>>> thread start..." + Thread.currentThread().getName() + "," + now);
  19. Thread thread = new Thread(() -> {
  20. System.out.println(">>>>>>>>>> testDaemonThread start..." + Thread.currentThread().getName());
  21. try {
  22. TimeUnit.SECONDS.sleep(2L);
  23. } catch (InterruptedException e) {
  24. e.printStackTrace();
  25. } finally {
  26. System.out.println(">>>>> testDaemonThread finally");
  27. }
  28. System.out.println(">>>>>>>>>> testDaemonThread end..." + Thread.currentThread().getName());
  29. }, "testDaemonThread");
  30. thread.setDaemon(true);
  31. thread.start();
  32. // thread.join();
  33. System.out.println(">>>>>>>>>> thread end..." + Thread.currentThread().getName()
  34. + " is daemon: " + Thread.currentThread().isDaemon() + "," + (System.currentTimeMillis() - now.getTime()));
  35. /*
  36. 结果:
  37. >>>>>>>>>> thread start...Test worker,Mon Aug 23 16:31:47 CST 2021
  38. >>>>>>>>>> thread end...Test worker is daemon: false,3
  39. >>>>>>>>>> testDaemonThread start...testDaemonThread
  40. */
  41. }
  42. /**
  43. * 测试非守护线程
  44. */
  45. @Test
  46. public void testNonDaemonThread() throws InterruptedException {
  47. Date now = new Date();
  48. System.out.println(">>>>>>>>>> thread start..." + Thread.currentThread().getName() + "," + now);
  49. Thread thread = new Thread(() -> {
  50. System.out.println(">>>>>>>>>> testNonDaemonThread start..." + Thread.currentThread().getName());
  51. try {
  52. TimeUnit.SECONDS.sleep(2L);
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. } finally {
  56. System.out.println(">>>>> testNonDaemonThread finally");
  57. }
  58. System.out.println(">>>>>>>>>> testNonDaemonThread end..." + Thread.currentThread().getName());
  59. }, "testDaemonThread");
  60. thread.setDaemon(false);
  61. thread.start();
  62. // thread.join();
  63. System.out.println(">>>>>>>>>> thread end..." + Thread.currentThread().getName()
  64. + " is daemon: " + Thread.currentThread().isDaemon() + "," + (System.currentTimeMillis() - now.getTime()));
  65. /*
  66. 结果:
  67. >>>>>>>>>> thread start...Test worker,Mon Aug 23 16:32:05 CST 2021
  68. >>>>>>>>>> thread end...Test worker is daemon: false,3
  69. >>>>>>>>>> testNonDaemonThread start...testDaemonThread
  70. */
  71. }
  72. public static void main(String[] args) throws InterruptedException {
  73. ThreadDemo demo = new ThreadDemo();
  74. // demo.testNonDaemonThread();
  75. demo.testDaemonThread();
  76. /*
  77. >>>>>>>>>> thread start...main,Mon Aug 23 16:33:43 CST 2021
  78. >>>>>>>>>> thread end...main is daemon: false,41
  79. >>>>>>>>>> testDaemonThread start...testDaemonThread
  80. */
  81. }
  82. @Test
  83. public void test() {
  84. }
  85. }

2.2 案例结果

测试中发现,@Test 与 main 输出的结果不一样
testDaemonThread 方法结果:

  1. test 的:
  2. >>>>>>>>>> thread start...Test worker,Mon Aug 23 16:35:23 CST 2021
  3. >>>>>>>>>> thread end...Test worker is daemon: false,2
  4. >>>>>>>>>> testDaemonThread start...testDaemonThread
  5. -------------------
  6. main 的:
  7. >>>>>>>>>> thread start...main,Mon Aug 23 16:37:04 CST 2021
  8. >>>>>>>>>> thread end...main is daemon: false,43
  9. >>>>>>>>>> testDaemonThread start...testDaemonThread

testNonDaemonThread 方法结果:

  1. @Test
  2. >>>>>>>>>> thread start...Test worker,Mon Aug 23 16:37:32 CST 2021
  3. >>>>>>>>>> thread end...Test worker is daemon: false,2
  4. >>>>>>>>>> testNonDaemonThread start...testDaemonThread
  5. ---------------
  6. main 的:
  7. >>>>>>>>>> thread start...main,Mon Aug 23 16:38:26 CST 2021
  8. >>>>>>>>>> thread end...main is daemon: false,44
  9. >>>>>>>>>> testNonDaemonThread start...testDaemonThread
  10. >>>>> testNonDaemonThread finally
  11. >>>>>>>>>> testNonDaemonThread end...testDaemonThread

感觉有点奇怪。