fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Q q = new Q();
  14. new Producer(q);
  15. new Consumer(q);
  16. }
  17. static class Q {
  18. int n;
  19. boolean valueSet = false;
  20.  
  21. synchronized int get() {
  22. while (!valueSet) {
  23. try {
  24. wait();
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. System.out.println("Rec: " + n);
  30. valueSet = false;
  31. notify();
  32. return n;
  33. }
  34.  
  35. synchronized void put(int n) {
  36. while (valueSet) {
  37. try {
  38. wait();
  39. } catch (InterruptedException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. this.n = n;
  44. valueSet = true;
  45. System.out.println("Send: " + n);
  46. notify();
  47. }
  48. }
  49. static class Producer implements Runnable {
  50. Q q;
  51.  
  52. public Producer(Q q) {
  53. this.q = q;
  54. new Thread(this, "R").run();
  55. }
  56.  
  57. @Override
  58. public void run() {
  59. int i = 0;
  60. while (true) {
  61. q.put(i++);
  62. }
  63. }
  64. }
  65. static class Consumer implements Runnable {
  66. Q q;
  67.  
  68. public Consumer(Q q) {
  69. this.q = q;
  70. new Thread(this, "S").run();
  71. }
  72.  
  73. @Override
  74. public void run() {
  75. while (true) {
  76. q.get();
  77. }
  78. }
  79. }
  80. }
Time limit exceeded #stdin #stdout 5s 2317312KB
stdin
Standard input is empty
stdout
Send: 0