/** * 问题:有线程a.b.c,如何让它们顺序执行? * 方式一:可用Join()方法实现 * 方式二:可用newSingleThreadExecutor() * Created by Smile on 2018/8/12. */ public class ThreadByOrder { public static void main(String[] args) throws InterruptedException { Thread a = new Thread(new ThreadTe
1.使用synchronized悲观锁(秋招阿里的一个笔试题,应该写的比较复杂,然后就没有然后了o(╥﹏╥)o) public class ThreadThreadp { private int flag = 0; public synchronized void printa() throws InterruptedException { while (true) { if(flag ==0) { System.out.print("A"); flag = 1; notifyAll(
[背景]这个题目是当时远程面试时,手写的题目.自己比较惭愧,当时写的并不好,面试完就又好好的完善了下. 一.题意分析 3个线程要按顺序执行,就要通过线程通信去控制这3个线程的执行顺序. 而线程通信的方式就有wait/notify, condition#await, condition#signal等 二.具体代码 public class Main { private static String lastCompleteStr = "C"; public static void mai