【问题标题】:Print Zero Even Odd problem of leetcode shows error Time Limit Exceededleetcode打印零偶奇问题显示错误Time Limit Exceeded
【发布时间】:2020-03-15 18:41:50
【问题描述】:

我为打印零偶奇问题的leetcode问题尝试了以下代码。 在测试最简单的输入时,它向我显示 Time Limit Exceeded 错误。 有人可以提出一些建议吗? https://leetcode.com/problems/print-zero-even-odd

class ZeroEvenOdd {

    private int n;

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

    // printNumber.accept(x) outputs "x", where x is an integer.
    boolean z = true;
    public void zero(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i <= n; i++) {
            if(z) {
                printNumber.accept(0);
            }
            z = false;
            odd = !odd;
            try { notifyAll(); } catch(Exception e){}
            do { try { wait(); } catch(Exception e){ } } while(!z);
        }
    }
    boolean odd = false;
    public void even(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i <= n; i++) {
            do { try { wait(); } catch(Exception e){ } } while(z || odd);
            if(!odd && !z && i%2 == 0) {
                printNumber.accept(i);
            }
            z = true;
            try { notifyAll(); } catch(Exception e){}
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        for(int i = 1; i <= n; i++) {
            do { try { wait(); } catch(Exception e){ } } while(z || !odd);
            if(odd && !z && i%2 == 1) {
                printNumber.accept(i);
            }
            z = true;
            try { notifyAll(); } catch(Exception e){}
        }
    }
   }

【问题讨论】:

    标签: java multithreading


    【解决方案1】:

    尝试使用synchronized:代码看起来更简单

    class ZeroEvenOdd {
        private int n;
    
        public ZeroEvenOdd(int n) {
            this.n = n;
        }
    
        private enum STATUS{
            ZERO, ODD, EVEN;
        }
    
        private STATUS status = STATUS.ZERO;
        private int x = 0;
        private Object lock = new Object();
    
        // printNumber.accept(x) outputs "x", where x is an integer.
        public void zero(IntConsumer printNumber) throws InterruptedException {
            while(x<=n){
                switch(status){
                    case ZERO:
                        synchronized(lock){
                            if(x < n){
                                printNumber.accept(0);    
                            }
                            if(x%2==0){
                                status = STATUS.ODD;
                            }else{
                                status = STATUS.EVEN;
                            }
                            x++;
                        }
                        break;
                }
            }
        }
    
        public void even(IntConsumer printNumber) throws InterruptedException {
            while(x<=n){
                switch(status){
                    case EVEN:
                        if (x<=n){
                            printNumber.accept(x);
                        }
                        status = STATUS.ZERO;
                    break;
                }
            }
        }
    
        public void odd(IntConsumer printNumber) throws InterruptedException {
    
            while(x<=n){
                switch(status){
                    case ODD:
                        synchronized(lock){
                            if(x <= n){
                                printNumber.accept(x);
                            }
                            status = STATUS.ZERO;
                        }
                        break;
                }
            }
        }
    }
    

    如需synchronized的完整参考,请访问this

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2015-12-04
      • 1970-01-01
      相关资源
      最近更新 更多