2年大廠經(jīng)驗(yàn),面試還是太緊張,90%的人不會寫

public class LoopPrintString { static Object lock = new Object(); static AtomicInteger i = new AtomicInteger(0); public static void main(String[] args) { new Thread(() -> { synchronized (lock) { while (i.getAndIncrement() < 100) { System.out.println("a-" + i.get()); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"a").start(); new Thread(()->{ try { Thread.sleep(10); synchronized (lock){ while (i.getAndIncrement() < 100){ System.out.println("b-" + i.get()); if(i.get() == 100) { lock.notify(); continue; } lock.notify(); lock.wait(); } } } catch (InterruptedException e) { e.printStackTrace(); } },"b").start();
面向題目編程
標(biāo)簽: