欢迎光临散文网 会员登陆 & 注册

2年大厂经验,面试还是太紧张,90%的人不会写

2023-08-01 21:16 作者:Annshyc  | 我要投稿
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();

面向题目编程

2年大厂经验,面试还是太紧张,90%的人不会写的评论 (共 条)

分享到微博请遵守国家法律