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】
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- linux笔记|linux 常用命令汇总(面向面试)
- jvm|【JVM】JVM08(java内存模型解析[JMM])