簡體   English   中英

使用兩個線程打印偶數和奇數

[英]Printing even and odd number using two threads

我寫了下面的程序來打印偶數和奇數:

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");
            }
        }
    }
}

它給我的 Output 是:

內印奇數

奇數電流值為0

奇數是 1

通知從奇數執行

內印奇數

奇數電流值為 1

奇數電流值為 1,電流 %2 值為 1

奇怪的等待

內部打印甚至

偶數中的電流值為1

甚至沒有。 是 2

通知從偶數執行

內部打印甚至

偶數中的電流值為2

甚至等待

奇數電流值為 1,電流 %2 值為 1

奇怪的等待

我期望兩個線程使用waitnotify機制輪流打印偶數和奇數。 我究竟做錯了什么? 我也嘗試過使current變量易失,但它給出了相同的 output。

在這種情況下while (current % 2 != 0) (和PrintEven中的相反值)時, current的值不會更新。 使用while (currentValue.getCurrent() % 2 != 0)代替; 擺脫current變量或在循環中更新它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM