面试|【面试】--三个线程轮流打印ABC

0 synchronized 实现

public class SynchronizeABC extends Thread {private String name; private Object pre; private Object self; public SynchronizeABC(String name, Object pre, Object self) { this.name = name; this.pre = pre; this.self = self; }@Override public void run() { for (; ; ) { synchronized (pre) { synchronized (self) { System.out.println(name); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } self.notifyAll(); } try { pre.wait(); System.out.println("wait "+name); } catch (InterruptedException e) { e.printStackTrace(); } }} }public static void main(String[] args) throws InterruptedException { Object a = new Object(); Object b = new Object(); Object c = new Object(); new SynchronizeABC("A", c, a).start(); Thread.sleep(500); new SynchronizeABC("B", a, b).start(); Thread.sleep(500); new SynchronizeABC("C", b, c).start(); Thread.currentThread().join(); } }



1 lock实现

public class ABCLock {private static Lock lock = new ReentrantLock(); private static Condition conditionA = lock.newCondition(); private static Condition conditionB = lock.newCondition(); private static Condition conditionC = lock.newCondition(); private static int count = 1; private static Thread threadA = new Thread(new Runnable() { @Override public void run() {while (true) { try { lock.lock(); while (count % 3 == 1) {System.out.print("A"); Thread.sleep(500); count++; }} catch (Exception e) {} finally { lock.unlock(); }} } }); private static Thread threadB = new Thread(new Runnable() { @Override public void run() { while (true) {try { lock.lock(); while (count % 3 == 2) {System.out.print("B"); Thread.sleep(500); count++; }} catch (Exception e) {} finally { lock.unlock(); } } }}); private static Thread threadC = new Thread(new Runnable() { @Override public void run() { while (true) {try { lock.lock(); while (count % 3 == 0) {System.out.print("C"); Thread.sleep(500); count++; }} catch (Exception e) {} finally { lock.unlock(); }} } }); public static void main(String[] args) throws InterruptedException { threadA.start(); threadB.start(); threadC.start(); Thread.currentThread().join(); } }



2 Lock + Condition

public class ABCLock {private static Lock lock = new ReentrantLock(); private static Condition conditionA = lock.newCondition(); private static Condition conditionB = lock.newCondition(); private static Condition conditionC = lock.newCondition(); private static int count = 1; private static Thread threadA = new Thread(new Runnable() { @Override public void run() {while (true) { try { lock.lock(); while (count % 3 != 1) conditionA.wait(); System.out.print("A"); Thread.sleep(500); count++; conditionB.signal(); } catch (Exception e) {} finally { lock.unlock(); }} } }); private static Thread threadB = new Thread(new Runnable() { @Override public void run() { while (true) {try { lock.lock(); while (count % 3 != 2) conditionB.wait(); System.out.print("B"); Thread.sleep(500); count++; conditionC.signal(); } catch (Exception e) {} finally { lock.unlock(); } } }}); private static Thread threadC = new Thread(new Runnable() { @Override public void run() { while (true) {try { lock.lock(); while (count % 3 != 0) conditionC.wait(); System.out.print("C"); Thread.sleep(500); count++; conditionA.signal(); } catch (Exception e) {} finally { lock.unlock(); }} } }); public static void main(String[] args) throws InterruptedException { threadA.start(); threadB.start(); threadC.start(); Thread.currentThread().join(); } }



3 信号量

public class ABCSemaphore { private static Semaphore semaphoreA = new Semaphore(1); private static Semaphore semaphoreB = new Semaphore(0); private static Semaphore semaphoreC = new Semaphore(0); private static Thread threadA = new Thread(new Runnable() { @Override public void run() {while (true) { try { semaphoreA.acquire(); System.out.print("A"); Thread.sleep(500); semaphoreB.release(); } catch (Exception e) {} finally {}} } }); private static Thread threadB = new Thread(new Runnable() { @Override public void run() { while (true) { try { semaphoreB.acquire(); System.out.print("B"); Thread.sleep(500); semaphoreC.release(); } catch (Exception e) {} finally {}} }}); private static Thread threadC = new Thread(new Runnable() { @Override public void run() { while (true) { try { semaphoreC.acquire(); System.out.print("C"); Thread.sleep(500); semaphoreA.release(); } catch (Exception e) {} finally {}}}}); public static void main(String[] args) throws InterruptedException { threadA.start(); threadB.start(); threadC.start(); Thread.currentThread().join(); }}

【面试|【面试】--三个线程轮流打印ABC】

    推荐阅读