1. public class Test1 {
    2. public static void main(String[] args) throws Exception {
    3. Object a = new Object();
    4. Object b = new Object();
    5. Object c = new Object();
    6. new Thread(new MyRunnable("A", c, a)).start();
    7. Thread.sleep(100);
    8. new Thread(new MyRunnable("B", a, b)).start();
    9. Thread.sleep(100);
    10. new Thread(new MyRunnable("C", b, c)).start();
    11. }
    12. }
    13. class MyRunnable implements Runnable {
    14. private String name;
    15. private Object prev;
    16. private Object cur;
    17. public MyRunnable(String name, Object prev, Object cur) {
    18. this.name = name;
    19. this.prev = prev;
    20. this.cur = cur;
    21. }
    22. @Override
    23. public void run() {
    24. int count = 10;
    25. while (count > 0) {
    26. synchronized (prev) {
    27. synchronized (cur) {
    28. System.out.print(name);
    29. count--;
    30. cur.notifyAll();
    31. }
    32. if (count != 0) {
    33. try {
    34. prev.wait();
    35. } catch (InterruptedException e) {
    36. e.printStackTrace();
    37. }
    38. }
    39. }
    40. }
    41. }
    42. }
    1. public class Test1 {
    2. private static volatile int state = 0;
    3. static class ThreadA extends Thread {
    4. @Override
    5. public void run() {
    6. int i = 0;
    7. while (i < 10) {
    8. if (state % 3 == 0) {
    9. System.out.print("A");
    10. state++;
    11. i++;
    12. }
    13. }
    14. }
    15. }
    16. static class ThreadB extends Thread {
    17. @Override
    18. public void run() {
    19. int i = 0;
    20. while (i < 10) {
    21. if (state % 3 == 1) {
    22. System.out.print("B");
    23. state++;
    24. i++;
    25. }
    26. }
    27. }
    28. }
    29. static class ThreadC extends Thread {
    30. @Override
    31. public void run() {
    32. int i = 0;
    33. while (i < 10) {
    34. if (state % 3 == 2) {
    35. System.out.print("C");
    36. state++;
    37. i++;
    38. }
    39. }
    40. }
    41. }
    42. public static void main(String[] args) {
    43. new ThreadA().start();
    44. new ThreadB().start();
    45. new ThreadC().start();
    46. }
    47. }
    1. public class Test1 {
    2. private static Lock lock = new ReentrantLock();
    3. private static Condition A = lock.newCondition();
    4. private static Condition B = lock.newCondition();
    5. private static Condition C = lock.newCondition();
    6. private static int count = 0;
    7. static class ThreadA extends Thread {
    8. @Override
    9. public void run() {
    10. try {
    11. lock.lock();
    12. for (int i = 0; i < 10; i++) {
    13. while (count % 3 != 0)
    14. A.await();
    15. System.out.print("A");
    16. count++;
    17. B.signal();
    18. }
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. } finally {
    22. lock.unlock();
    23. }
    24. }
    25. }
    26. static class ThreadB extends Thread {
    27. @Override
    28. public void run() {
    29. try {
    30. lock.lock();
    31. for (int i = 0; i < 10; i++) {
    32. while (count % 3 != 1)
    33. B.await();
    34. System.out.print("B");
    35. count++;
    36. C.signal();
    37. }
    38. } catch (Exception e) {
    39. e.printStackTrace();
    40. } finally {
    41. lock.unlock();
    42. }
    43. }
    44. }
    45. static class ThreadC extends Thread {
    46. @Override
    47. public void run() {
    48. try {
    49. lock.lock();
    50. for (int i = 0; i < 10; i++) {
    51. while (count % 3 != 2)
    52. C.await();
    53. System.out.print("C");
    54. count++;
    55. A.signal();
    56. }
    57. } catch (Exception e) {
    58. e.printStackTrace();
    59. } finally {
    60. lock.unlock();
    61. }
    62. }
    63. }
    64. public static void main(String[] args) {
    65. new ThreadA().start();
    66. new ThreadB().start();
    67. new ThreadC().start();
    68. }
    69. }
    1. public class Test1 {
    2. private static Semaphore A = new Semaphore(1);
    3. private static Semaphore B = new Semaphore(0);
    4. private static Semaphore C = new Semaphore(0);
    5. static class ThreadA extends Thread {
    6. @SneakyThrows
    7. @Override
    8. public void run() {
    9. for (int i = 0; i < 10; i++) {
    10. A.acquire();
    11. System.out.print("A");
    12. B.release();
    13. }
    14. }
    15. }
    16. static class ThreadB extends Thread {
    17. @SneakyThrows
    18. @Override
    19. public void run() {
    20. for (int i = 0; i < 10; i++) {
    21. B.acquire();
    22. System.out.print("B");
    23. C.release();
    24. }
    25. }
    26. }
    27. static class ThreadC extends Thread {
    28. @SneakyThrows
    29. @Override
    30. public void run() {
    31. for (int i = 0; i < 10; i++) {
    32. C.acquire();
    33. System.out.println("C");
    34. A.release();
    35. }
    36. }
    37. }
    38. public static void main(String[] args) {
    39. new ThreadA().start();
    40. new ThreadB().start();
    41. new ThreadC().start();
    42. }
    43. }