public class Test1 { public static void main(String[] args) throws Exception { Object a = new Object(); Object b = new Object(); Object c = new Object(); new Thread(new MyRunnable("A", c, a)).start(); Thread.sleep(100); new Thread(new MyRunnable("B", a, b)).start(); Thread.sleep(100); new Thread(new MyRunnable("C", b, c)).start(); }}class MyRunnable implements Runnable { private String name; private Object prev; private Object cur; public MyRunnable(String name, Object prev, Object cur) { this.name = name; this.prev = prev; this.cur = cur; } @Override public void run() { int count = 10; while (count > 0) { synchronized (prev) { synchronized (cur) { System.out.print(name); count--; cur.notifyAll(); } if (count != 0) { try { prev.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }}
public class Test1 { private static volatile int state = 0; static class ThreadA extends Thread { @Override public void run() { int i = 0; while (i < 10) { if (state % 3 == 0) { System.out.print("A"); state++; i++; } } } } static class ThreadB extends Thread { @Override public void run() { int i = 0; while (i < 10) { if (state % 3 == 1) { System.out.print("B"); state++; i++; } } } } static class ThreadC extends Thread { @Override public void run() { int i = 0; while (i < 10) { if (state % 3 == 2) { System.out.print("C"); state++; i++; } } } } public static void main(String[] args) { new ThreadA().start(); new ThreadB().start(); new ThreadC().start(); }}
public class Test1 { private static Lock lock = new ReentrantLock(); private static Condition A = lock.newCondition(); private static Condition B = lock.newCondition(); private static Condition C = lock.newCondition(); private static int count = 0; static class ThreadA extends Thread { @Override public void run() { try { lock.lock(); for (int i = 0; i < 10; i++) { while (count % 3 != 0) A.await(); System.out.print("A"); count++; B.signal(); } } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } } static class ThreadB extends Thread { @Override public void run() { try { lock.lock(); for (int i = 0; i < 10; i++) { while (count % 3 != 1) B.await(); System.out.print("B"); count++; C.signal(); } } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } } static class ThreadC extends Thread { @Override public void run() { try { lock.lock(); for (int i = 0; i < 10; i++) { while (count % 3 != 2) C.await(); System.out.print("C"); count++; A.signal(); } } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } } public static void main(String[] args) { new ThreadA().start(); new ThreadB().start(); new ThreadC().start(); }}
public class Test1 { private static Semaphore A = new Semaphore(1); private static Semaphore B = new Semaphore(0); private static Semaphore C = new Semaphore(0); static class ThreadA extends Thread { @SneakyThrows @Override public void run() { for (int i = 0; i < 10; i++) { A.acquire(); System.out.print("A"); B.release(); } } } static class ThreadB extends Thread { @SneakyThrows @Override public void run() { for (int i = 0; i < 10; i++) { B.acquire(); System.out.print("B"); C.release(); } } } static class ThreadC extends Thread { @SneakyThrows @Override public void run() { for (int i = 0; i < 10; i++) { C.acquire(); System.out.println("C"); A.release(); } } } public static void main(String[] args) { new ThreadA().start(); new ThreadB().start(); new ThreadC().start(); }}