多线程问题

问问大佬们

(1)
class ZeroEvenOdd {
    private int n;
    ReentrantLock lock = new ReentrantLock(true);
    private int x = 1;
    private boolean isZero = true;
    private Condition condition = lock.newCondition();
    public ZeroEvenOdd(int n) {
        
        this.n = n;

    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        while(x <= n){
                    lock.lock();
        if(isZero&amp;&amp;x <= n){
            printNumber.accept(0);
            isZero = false;
            condition.signalAll();
        }else{
            condition.await();
        }
        lock.unlock();
        }

    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while(x <= n){
        lock.lock();
        if(!isZero &amp;&amp; x%2 == 0){
            printNumber.accept(x);
            x++;
            isZero = true;
            condition.signalAll();
        }else{
            condition.await();
        }
        lock.unlock();
        }       
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while(x <= n){
        lock.lock();
        if(!isZero &amp;&amp; x%2 == 1){
            printNumber.accept(x);
            x++;
            isZero = true;
            condition.signalAll();
        }else{
            condition.await();
        }
        lock.unlock();
        }    
    }
}

(2)class ZeroEvenOdd {
    private final int n;
    private int x = 1;
    // true: 打印0  false: 不打印0
    private boolean isZero = true;

    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    public void zero(IntConsumer printNumber) throws InterruptedException {
        while (x <= n) {
            synchronized (this) {
                if (isZero) {
                    printNumber.accept(0);
                    isZero = false;
                    this.notifyAll();
                } else {
                    this.wait();
                }
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        while (x <= n) {
            synchronized (this) {
                if (x % 2 == 0 &amp;&amp; !isZero) {
                    printNumber.accept(x);
                    isZero = true;
                    x++;
                    this.notifyAll();
                } else {
                    this.wait();
                }
            }
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        while (x <= n) {
            synchronized (this) {
                if (x % 2 != 0 &amp;&amp; !isZero) {
                    printNumber.accept(x);
                    isZero = true;
                    x++;
                    this.notifyAll();
                } else {
                    this.wait();
                }
            }
        }
    }
}

为什么(1)代码最后可能多输出一个0。(2)代码不会
全部评论

相关推荐

求过求过
想玩飞盘的菠萝蜜在春...:流程这么快吗兄弟
点赞 评论 收藏
分享
背景&nbsp;双一流本硕&nbsp;双非大圆满&nbsp;只找游戏开发相关的岗位。&nbsp;8&nbsp;月初开始秋招到现在&nbsp;投了四五十家吧,&nbsp;目前两&nbsp;offer,&nbsp;不打算继续投了,把剩下的流程走完就开始沉淀了。目前两&nbsp;offer&nbsp;一个是网易互娱测开&nbsp;base&nbsp;广州,一个是江娱互动客户端开发&nbsp;base&nbsp;北京。应该确定网易这个了,说实话北京这个我挺想去的,这家的产品和工作氛围我了解了也不错,是那种踏实做事的,可惜我是广东人。网易的测开是调剂的二志愿,看了下有内部转岗机会,所以打算后面找个时间提前实习,沉淀下再做一个&nbsp;demo&nbsp;作品,写一些&nbsp;shader,增强下图形学渲染的能力,再学点编辑器开发。看到时候内部转岗或者春招继续投客户端开发这样。后面还能再动摇的话应该就灵犀或者腾子了吧(假如这两家确认的是客户端开发岗的话)。-----------------------补下timeline网易互娱&nbsp;测开&nbsp;8.2笔试&nbsp;&nbsp;8.21&nbsp;技术面&nbsp;&nbsp;8.29&nbsp;leader&amp;HRBP面(终面)&nbsp;9.8&nbsp;录用审核(之前一直显示面试中)9.14&nbsp;oc江娱互动&nbsp;客户端开发&nbsp;8.29主程面&nbsp;9.3&nbsp;制作人面&nbsp;9.5&nbsp;BOSS面&nbsp;9.11&nbsp;口头OC&nbsp;9.15&nbsp;正式offer后面考虑了一下&nbsp;&nbsp;感觉还是能走开发就开发吧,测开不太感兴趣,要内部活水转岗还要满1年才能申请。。
点赞 评论 收藏
分享
09-02 11:14
已编辑
四川大学 Java
吴offer选手:这种面试是最烦的,学不到东西,然后还被挂的莫名其妙。之前看到一种说法是面试官如果不想要你了,就会问一些很简单的问题,防止你举报他
点赞 评论 收藏
分享
评论
2
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务