同步方法里面可以调用非同步方法

  1. public class T1 {
  2. // 示例演示同步方法和非同步方法同时调用
  3. synchronized void m() {
  4. System.out.println("synchronized method" + new Date());
  5. n();
  6. }
  7. void n() {
  8. System.out.println("not synchronized method" + new Date());
  9. }
  10. public static void main(String[] args) throws InterruptedException {
  11. CountDownLatch start = new CountDownLatch(1);
  12. CountDownLatch end = new CountDownLatch(10);
  13. T1 t = new T1();
  14. for (int i = 0; i < 10; i++) {
  15. new Thread(() -> {
  16. try {
  17. start.await();
  18. t.m();
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. } finally {
  22. end.countDown();
  23. }
  24. }).start();
  25. }
  26. start.countDown();
  27. end.await();
  28. }
  29. }
  30. 输出结果:
  31. synchronized methodThu Oct 29 13:43:37 CST 2020
  32. not synchronized methodThu Oct 29 13:43:37 CST 2020
  33. synchronized methodThu Oct 29 13:43:37 CST 2020
  34. not synchronized methodThu Oct 29 13:43:37 CST 2020
  35. synchronized methodThu Oct 29 13:43:37 CST 2020
  36. not synchronized methodThu Oct 29 13:43:37 CST 2020
  37. synchronized methodThu Oct 29 13:43:37 CST 2020
  38. not synchronized methodThu Oct 29 13:43:37 CST 2020
  39. synchronized methodThu Oct 29 13:43:37 CST 2020
  40. not synchronized methodThu Oct 29 13:43:37 CST 2020
  41. synchronized methodThu Oct 29 13:43:37 CST 2020
  42. not synchronized methodThu Oct 29 13:43:37 CST 2020
  43. synchronized methodThu Oct 29 13:43:37 CST 2020
  44. not synchronized methodThu Oct 29 13:43:37 CST 2020
  45. synchronized methodThu Oct 29 13:43:37 CST 2020
  46. not synchronized methodThu Oct 29 13:43:37 CST 2020
  47. synchronized methodThu Oct 29 13:43:37 CST 2020
  48. not synchronized methodThu Oct 29 13:43:37 CST 2020
  49. synchronized methodThu Oct 29 13:43:37 CST 2020
  50. not synchronized methodThu Oct 29 13:43:37 CST 2020

同步方法和非同步方法可同时调用

  1. public class T1 {
  2. // 示例演示同步方法和非同步方法同时调用
  3. synchronized void m() {
  4. System.out.println("synchronized method" + new Date());
  5. }
  6. void n() {
  7. System.out.println("not synchronized method" + new Date());
  8. }
  9. public static void main(String[] args) throws InterruptedException {
  10. CountDownLatch start = new CountDownLatch(1);
  11. CountDownLatch end = new CountDownLatch(10);
  12. T1 t = new T1();
  13. for (int i = 0; i < 10; i++) {
  14. new Thread(() -> {
  15. try {
  16. start.await();
  17. t.m();
  18. t.n();
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. } finally {
  22. end.countDown();
  23. }
  24. }).start();
  25. new Thread(() -> {
  26. try {
  27. start.await();
  28. t.m();
  29. t.n();
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. } finally {
  33. end.countDown();
  34. }
  35. }).start();
  36. }
  37. start.countDown();
  38. end.await();
  39. }
  40. }
  41. 输出结果
  42. synchronized methodThu Oct 29 13:44:28 CST 2020
  43. not synchronized methodThu Oct 29 13:44:28 CST 2020
  44. synchronized methodThu Oct 29 13:44:28 CST 2020
  45. not synchronized methodThu Oct 29 13:44:28 CST 2020
  46. synchronized methodThu Oct 29 13:44:28 CST 2020
  47. not synchronized methodThu Oct 29 13:44:28 CST 2020
  48. synchronized methodThu Oct 29 13:44:28 CST 2020
  49. not synchronized methodThu Oct 29 13:44:28 CST 2020
  50. synchronized methodThu Oct 29 13:44:28 CST 2020
  51. not synchronized methodThu Oct 29 13:44:28 CST 2020
  52. synchronized methodThu Oct 29 13:44:28 CST 2020
  53. not synchronized methodThu Oct 29 13:44:28 CST 2020
  54. synchronized methodThu Oct 29 13:44:28 CST 2020
  55. not synchronized methodThu Oct 29 13:44:28 CST 2020
  56. synchronized methodThu Oct 29 13:44:28 CST 2020
  57. not synchronized methodThu Oct 29 13:44:28 CST 2020
  58. synchronized methodThu Oct 29 13:44:28 CST 2020
  59. not synchronized methodThu Oct 29 13:44:28 CST 2020
  60. synchronized methodThu Oct 29 13:44:28 CST 2020
  61. not synchronized methodThu Oct 29 13:44:28 CST 2020
  62. synchronized methodThu Oct 29 13:44:28 CST 2020
  63. not synchronized methodThu Oct 29 13:44:28 CST 2020
  64. synchronized methodThu Oct 29 13:44:28 CST 2020
  65. not synchronized methodThu Oct 29 13:44:28 CST 2020
  66. synchronized methodThu Oct 29 13:44:28 CST 2020
  67. not synchronized methodThu Oct 29 13:44:28 CST 2020
  68. synchronized methodThu Oct 29 13:44:28 CST 2020
  69. not synchronized methodThu Oct 29 13:44:28 CST 2020
  70. synchronized methodThu Oct 29 13:44:28 CST 2020
  71. not synchronized methodThu Oct 29 13:44:28 CST 2020
  72. synchronized methodThu Oct 29 13:44:28 CST 2020
  73. not synchronized methodThu Oct 29 13:44:28 CST 2020
  74. synchronized methodThu Oct 29 13:44:28 CST 2020
  75. not synchronized methodThu Oct 29 13:44:28 CST 2020
  76. synchronized methodThu Oct 29 13:44:28 CST 2020
  77. not synchronized methodThu Oct 29 13:44:28 CST 2020
  78. synchronized methodThu Oct 29 13:44:28 CST 2020
  79. not synchronized methodThu Oct 29 13:44:28 CST 2020
  80. synchronized methodThu Oct 29 13:44:28 CST 2020
  81. not synchronized methodThu Oct 29 13:44:28 CST 2020