首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用两个线程打印偶数和奇数

使用两个线程打印偶数和奇数
EN

Stack Overflow用户
提问于 2020-07-25 04:49:29
回答 1查看 128关注 0票数 0

我写了下面的程序打印偶数和奇数:

代码语言:javascript
复制
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");
            }
        }
    }
}

它给我的输出是:

内部打印奇数

奇数中电流的值为0

奇数号。是1

通知从奇数执行

内部打印奇数

奇数中电流的值为1

奇数中电流的值为1,电流%2值为1

奇数等待

内部打印甚至

电流的值为1

甚至没有。是2

通知从偶数执行

内部打印甚至

均匀电流的值为2

甚至等待

奇数中电流的值为1,电流%2值为1

奇数等待

我期望两个线程使用waitnotify机制轮流打印偶数和奇数。我做错了什么?我还尝试使current变量易失性,但它提供了相同的输出。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-25 05:49:48

在这种情况下,不更新while (current % 2 != 0) (和PrintEven中相反的)值。改用while (currentValue.getCurrent() % 2 != 0);去掉current变量或在循环中更新它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63084467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档