1. package com.lakala.mkst.service;
    2. import java.util.concurrent.locks.Condition;
    3. import java.util.concurrent.locks.ReentrantLock;
    4. public class Main {
    5. static ReentrantLock lock = new ReentrantLock();
    6. static Condition one = lock.newCondition();
    7. static Condition two = lock.newCondition();
    8. volatile static boolean printNum = true;
    9. public static void main(String[] args) {
    10. new Thread(() -> {
    11. for (int a = 1; a < 27; a++) {
    12. lock.lock();
    13. try {
    14. while (!printNum) {
    15. one.await();
    16. }
    17. System.out.print(a);
    18. printNum = false;
    19. two.signal();
    20. } catch (InterruptedException e) {
    21. e.printStackTrace();
    22. } finally {
    23. lock.unlock();
    24. }
    25. }
    26. }).start();
    27. new Thread(() -> {
    28. for (int a = 65; a <= 90; a++) {
    29. lock.lock();
    30. try {
    31. while (printNum) {
    32. two.await();
    33. }
    34. System.out.print((char) a);
    35. printNum = true;
    36. one.signal();
    37. } catch (InterruptedException e) {
    38. e.printStackTrace();
    39. } finally {
    40. lock.unlock();
    41. }
    42. }
    43. }).start();
    44. }
    45. }

    打印结果如下:
    1A2B3C4D5E6F7G8H9I10J11K12L13M14N15O16P17Q18R19S20T21U22V23W24X25Y26Z