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. class Q { int n; synchronized int get() { System.out.println("Got n :"+n); return n; } synchronized void put(int n) { this.n = n; System.out.println("Put n :"+n); } }
  8.  
  9. class Producer implements Runnable
  10. {
  11. Q q1;
  12. Producer(Q q)
  13. {
  14. this.q1 = q;
  15. new Thread(this).start();
  16. }
  17. public void run()
  18. {
  19. int i =0;
  20. q1.put(i++);
  21. }
  22. }
  23.  
  24. class Consumer implements Runnable
  25. {
  26. Q q1;
  27. Consumer(Q q)
  28. {
  29. this.q1 = q;
  30. new Thread(this).start();
  31. }
  32. public void run()
  33. {
  34. q1.get();
  35. }
  36. }
  37. /* Name of the class has to be "Main" only if the class is public. */
  38. class Ideone
  39. {
  40.  
  41.  
  42. public static void main (String[] args) throws java.lang.Exception
  43. {
  44. Q q = new Q();
  45. new Producer(q);
  46. new Consumer(q);
  47. }
  48. }
Success #stdin #stdout 0.1s 321152KB
stdin
Standard input is empty
stdout
Put n :0
Got n :0