多线程问题

问问大佬们

(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)代码不会
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-18 18:23
点赞 评论 收藏
分享
风中翠竹:真的真的真的没有kpi。。。面试官是没有任何kpi的,捞是真的想试试看这个行不行,碰碰运气,或者是面试官比较闲现在,没事捞个人看看。kpi算HR那边,但是只有你入职了,kpi才作数,面试是没有的。
双非有机会进大厂吗
点赞 评论 收藏
分享
评论
2
4
分享

创作者周榜

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