[英]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:它给我的 Output 是:
Inside Print odd内印奇数
Value of current in odd is 0奇数电流值为0
Odd no.奇数is 1是 1
Notify executed from odd通知从奇数执行
Inside Print odd内印奇数
Value of current in odd is 1奇数电流值为 1
Value of current in odd is 1and value of current % 2 is 1奇数电流值为 1,电流 %2 值为 1
odd waiting奇怪的等待
Inside Print even内部打印甚至
Value of current in even is 1偶数中的电流值为1
Even no.甚至没有。 is 2是 2
Notify executed from even通知从偶数执行
Inside Print even内部打印甚至
Value of current in even is 2偶数中的电流值为2
even waiting甚至等待
Value of current in odd is 1and value of current % 2 is 1奇数电流值为 1,电流 %2 值为 1
odd waiting奇怪的等待
I am expecting both threads to print even and odd numbers taking turns using wait
and notify
mechanism.我期望两个线程使用wait
和notify
机制轮流打印偶数和奇数。 What am I doing wrong?我究竟做错了什么? I also tried with making the current
variable volatile, but it gives the same output.我也尝试过使current
变量易失,但它给出了相同的 output。
In this condition while (current % 2 != 0)
(and opposite one in PrintEven
) value of current
is not updated.在这种情况下while (current % 2 != 0)
(和PrintEven
中的相反值)时, current
的值不会更新。 Use while (currentValue.getCurrent() % 2 != 0)
instead;使用while (currentValue.getCurrent() % 2 != 0)
代替; get rid of the current
variable or update it in the loop.摆脱current
变量或在循环中更新它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.