多线程|多线程面试题——交替打印1和2

public class Multi { public static void main(String[] args) throws Exception { final Object obj = new Object(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { while (true) { synchronized (obj) { obj.notifyAll(); obj.wait(); System.out.println(1); } } } catch (Exception e) {e.printStackTrace(); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { try { while (true) { synchronized (obj) { obj.notifyAll(); obj.wait(); System.out.println(2); } } } catch (Exception e) {e.printStackTrace(); } } }); t1.start(); t2.start(); }; }

    推荐阅读