Odd even number printing using thread

前端 未结 13 2018
挽巷
挽巷 2020-11-29 08:39

Odd even number printing using thread.Create one thread class, two instance of the thread. One will print the odd number and the other will print the even number.

13条回答
  •  既然无缘
    2020-11-29 09:16

    Your code corrected with using Lock interface:

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class NumberPrinter implements Runnable {
        private Lock lock;
        private Condition condition;
        private String type;
        private static boolean oddTurn = true;
    
        public NumberPrinter(String type, Lock lock, Condition condition) {
            this.type = type;
            this.lock = lock;
            this.condition = condition;
        }
    
        public void run() {
            int i = type.equals("odd") ? 1 : 2;
            while (i <= 10) {
                if (type.equals("odd"))
                    printOdd(i);
                if (type.equals("even"))
                    printEven(i);
                i = i + 2;
            }
        }
    
        private void printOdd(int i) {
            // synchronized (lock) {
            lock.lock();
            while (!oddTurn) {
                try {
                    // lock.wait();
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(type + " " + i);
            oddTurn = false;
            // lock.notifyAll();
            condition.signalAll();
            lock.unlock();
        }
    
        // }
    
        private void printEven(int i) {
            // synchronized (lock) {
            lock.lock();
            while (oddTurn) {
                try {
                    // lock.wait();
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(type + " " + i);
            oddTurn = true;
            // lock.notifyAll();
            condition.signalAll();
            lock.unlock();
        }
    
        // }
    
        public static void main(String[] args) {
            Lock lock = new ReentrantLock();
            Condition condition = lock.newCondition();
            Thread odd = new Thread(new NumberPrinter("odd", lock, condition));
            Thread even = new Thread(new NumberPrinter("even", lock, condition));
            odd.start();
            even.start();
        }
    }
    

提交回复
热议问题