英文:
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");
}
}
}
}
它给我输出的结果是:
**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**
我期望两个线程交替打印奇数和偶数,使用`wait`和`notify`机制。我做错了什么?我还尝试过将`current`变量设为`volatile`,但输出结果相同。
英文:
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.
答案1
得分: 1
在这个条件中 while (current % 2 != 0)
(以及在 PrintEven
中相反的条件),current
的值没有被更新。请改为使用 while (currentValue.getCurrent() % 2 != 0)
;要么移除 current
变量,要么在循环中更新它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论