简体   繁体   中英

Printing even and odd number using two threads

I have written below program to print even and odd numbers:

public class PrintEvenOdd {

    public static void main(String[] args) {
        CurrentValue currentValue = new CurrentValue();
        Thread oddThread = new Thread(new PrintOdd(10, currentValue));
        Thread evenThread = new Thread(new PrintEven(10, currentValue));
        oddThread.start();
        evenThread.start();
    }

}

class CurrentValue {

    private int current = 0;

    public int getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }
}


class PrintOdd implements Runnable {

    private int noOfValuesToPrint;
    private CurrentValue currentValue;

    public PrintOdd(int noOfValuesToPrint, CurrentValue currentValue) {
        this.noOfValuesToPrint = noOfValuesToPrint;
        this.currentValue = currentValue;
    }

    public void run() {
        while (true) {
            synchronized (currentValue) {
                System.out.println("Inside Print odd");
                int current = currentValue.getCurrent();
                System.out.println("Value of current in odd is " + current);
                while (current % 2 != 0) {
                    try {
                        System.out.println("Value of current in odd is " + current + "and value of current % 2  is "
                                + current % 2);
                        System.out.println("odd waiting");
                        currentValue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("Odd no. is " + ++current);
                currentValue.setCurrent(current);
                currentValue.notify();
                System.out.println("Notify executed from odd");
            }
        }
    }
}

class PrintEven implements Runnable {

    private int noOfValuesToPrint;
    private CurrentValue currentValue;

    public PrintEven(int noOfValuesToPrint, CurrentValue currentValue) {
        this.noOfValuesToPrint = noOfValuesToPrint;
        this.currentValue = currentValue;
    }

    public void run() {
        while (true) {
            synchronized (currentValue) {
                System.out.println("Inside Print even");
                int current = currentValue.getCurrent();
                System.out.println("Value of current in even is " + current);
                while (current % 2 == 0) {
                    try {
                        System.out.println("even waiting");
                        currentValue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("Even no. is " + ++current);
                currentValue.setCurrent(current);
                currentValue.notify();
                System.out.println("Notify executed from even");
            }
        }
    }
}

The Output it gives me is:

Inside Print odd

Value of current in odd is 0

Odd no.is 1

Notify executed from odd

Inside Print odd

Value of current in odd is 1

Value of current in odd is 1and value of current % 2 is 1

odd waiting

Inside Print even

Value of current in even is 1

Even no. is 2

Notify executed from even

Inside Print even

Value of current in even is 2

even waiting

Value of current in odd is 1and value of current % 2 is 1

odd waiting

I am expecting both threads to print even and odd numbers taking turns using wait and notify mechanism. What am I doing wrong? I also tried with making the current variable volatile, but it gives the same output.

In this condition while (current % 2 != 0) (and opposite one in PrintEven ) value of current is not updated. Use while (currentValue.getCurrent() % 2 != 0) instead; get rid of the current variable or update it in the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM