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(…
package ThreadABC; public class MyThread extends Thread { public static int status = 0; @Override public void run() { while (true) { synchronized (MyThread.class) { if ("A".equals(Thread.currentThread().getName()) && status < 30 &…
先来看一个例子: public class VolatileTest { public static void main(String[] args) { ThreadDemo td = new ThreadDemo(); new Thread(td).start(); while (true) { if (td.isFlag()) { Sy…