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

huangapple go评论96阅读模式
英文:

Printing even and odd number using two threads

问题

  1. 我已经编写了下面的程序来打印奇数和偶数
  2. public class PrintEvenOdd {
  3. public static void main(String[] args) {
  4. CurrentValue currentValue = new CurrentValue();
  5. Thread oddThread = new Thread(new PrintOdd(10, currentValue));
  6. Thread evenThread = new Thread(new PrintEven(10, currentValue));
  7. oddThread.start();
  8. evenThread.start();
  9. }
  10. }
  11. class CurrentValue {
  12. private int current = 0;
  13. public int getCurrent() {
  14. return current;
  15. }
  16. public void setCurrent(Integer current) {
  17. this.current = current;
  18. }
  19. }
  20. class PrintOdd implements Runnable {
  21. private int noOfValuesToPrint;
  22. private CurrentValue currentValue;
  23. public PrintOdd(int noOfValuesToPrint, CurrentValue currentValue) {
  24. this.noOfValuesToPrint = noOfValuesToPrint;
  25. this.currentValue = currentValue;
  26. }
  27. public void run() {
  28. while (true) {
  29. synchronized (currentValue) {
  30. System.out.println("Inside Print odd");
  31. int current = currentValue.getCurrent();
  32. System.out.println("Value of current in odd is " + current);
  33. while (current % 2 != 0) {
  34. try {
  35. System.out.println("Value of current in odd is " + current + "and value of current % 2 is "
  36. + current % 2);
  37. System.out.println("odd waiting");
  38. currentValue.wait();
  39. } catch (InterruptedException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. System.out.println("Odd no. is " + ++current);
  44. currentValue.setCurrent(current);
  45. currentValue.notify();
  46. System.out.println("Notify executed from odd");
  47. }
  48. }
  49. }
  50. }
  51. class PrintEven implements Runnable {
  52. private int noOfValuesToPrint;
  53. private CurrentValue currentValue;
  54. public PrintEven(int noOfValuesToPrint, CurrentValue currentValue) {
  55. this.noOfValuesToPrint = noOfValuesToPrint;
  56. this.currentValue = currentValue;
  57. }
  58. public void run() {
  59. while (true) {
  60. synchronized (currentValue) {
  61. System.out.println("Inside Print even");
  62. int current = currentValue.getCurrent();
  63. System.out.println("Value of current in even is " + current);
  64. while (current % 2 == 0) {
  65. try {
  66. System.out.println("even waiting");
  67. currentValue.wait();
  68. } catch (InterruptedException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. System.out.println("Even no. is " + ++current);
  73. currentValue.setCurrent(current);
  74. currentValue.notify();
  75. System.out.println("Notify executed from even");
  76. }
  77. }
  78. }
  79. }
  80. 它给我输出的结果是
  81. **Inside Print odd**
  82. **Value of current in odd is 0**
  83. **Odd no. is 1**
  84. **Notify executed from odd**
  85. **Inside Print odd**
  86. **Value of current in odd is 1**
  87. **Value of current in odd is 1and value of current % 2 is 1**
  88. **odd waiting**
  89. **Inside Print even**
  90. **Value of current in even is 1**
  91. **Even no. is 2**
  92. **Notify executed from even**
  93. **Inside Print even**
  94. **Value of current in even is 2**
  95. **even waiting**
  96. **Value of current in odd is 1and value of current % 2 is 1**
  97. **odd waiting**
  98. 我期望两个线程交替打印奇数和偶数使用`wait``notify`机制我做错了什么我还尝试过将`current`变量设为`volatile`但输出结果相同
英文:

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

  1. public class PrintEvenOdd {
  2. public static void main(String[] args) {
  3. CurrentValue currentValue = new CurrentValue();
  4. Thread oddThread = new Thread(new PrintOdd(10, currentValue));
  5. Thread evenThread = new Thread(new PrintEven(10, currentValue));
  6. oddThread.start();
  7. evenThread.start();
  8. }
  9. }
  10. class CurrentValue {
  11. private int current = 0;
  12. public int getCurrent() {
  13. return current;
  14. }
  15. public void setCurrent(Integer current) {
  16. this.current = current;
  17. }
  18. }
  19. class PrintOdd implements Runnable {
  20. private int noOfValuesToPrint;
  21. private CurrentValue currentValue;
  22. public PrintOdd(int noOfValuesToPrint, CurrentValue currentValue) {
  23. this.noOfValuesToPrint = noOfValuesToPrint;
  24. this.currentValue = currentValue;
  25. }
  26. public void run() {
  27. while (true) {
  28. synchronized (currentValue) {
  29. System.out.println("Inside Print odd");
  30. int current = currentValue.getCurrent();
  31. System.out.println("Value of current in odd is " + current);
  32. while (current % 2 != 0) {
  33. try {
  34. System.out.println("Value of current in odd is " + current + "and value of current % 2 is "
  35. + current % 2);
  36. System.out.println("odd waiting");
  37. currentValue.wait();
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. System.out.println("Odd no. is " + ++current);
  43. currentValue.setCurrent(current);
  44. currentValue.notify();
  45. System.out.println("Notify executed from odd");
  46. }
  47. }
  48. }
  49. }
  50. class PrintEven implements Runnable {
  51. private int noOfValuesToPrint;
  52. private CurrentValue currentValue;
  53. public PrintEven(int noOfValuesToPrint, CurrentValue currentValue) {
  54. this.noOfValuesToPrint = noOfValuesToPrint;
  55. this.currentValue = currentValue;
  56. }
  57. public void run() {
  58. while (true) {
  59. synchronized (currentValue) {
  60. System.out.println("Inside Print even");
  61. int current = currentValue.getCurrent();
  62. System.out.println("Value of current in even is " + current);
  63. while (current % 2 == 0) {
  64. try {
  65. System.out.println("even waiting");
  66. currentValue.wait();
  67. } catch (InterruptedException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. System.out.println("Even no. is " + ++current);
  72. currentValue.setCurrent(current);
  73. currentValue.notify();
  74. System.out.println("Notify executed from even");
  75. }
  76. }
  77. }
  78. }

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.

huangapple
  • 本文由 发表于 2020年7月25日 12:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63084467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定