fork(1) 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.  
  9. class MyClass
  10. {
  11. public static int count=0;
  12.  
  13. static boolean lock=false;
  14.  
  15. boolean getLock()
  16. {
  17. synchronized(this)
  18. {
  19. if(lock==false)
  20. {
  21. lock=true;
  22. return true;
  23. }
  24. else
  25. {
  26. return false;
  27. }
  28. }
  29. }
  30.  
  31.  
  32. public int getCount()
  33. {
  34.  
  35. System.out.println("Inside getcount()");
  36.  
  37.  
  38. return count;
  39.  
  40.  
  41. }
  42.  
  43.  
  44. public synchronized void incrementCount()
  45. {
  46.  
  47. count=count+1;
  48.  
  49. }
  50.  
  51. public synchronized void decrementCount()
  52. {
  53.  
  54. count=count-1;
  55.  
  56. }
  57. }
  58.  
  59. class Ideone
  60. {
  61.  
  62. public static int count=0;
  63.  
  64. static boolean lock=false;
  65.  
  66. boolean getLock()
  67. {
  68. synchronized(this)
  69. {
  70. if(lock==false)
  71. {
  72. lock=true;
  73. return true;
  74. }
  75. else
  76. {
  77. return false;
  78. }
  79. }
  80. }
  81.  
  82.  
  83. public int getCount()
  84. {
  85.  
  86. System.out.println("Inside getcount()");
  87.  
  88.  
  89. return count;
  90.  
  91.  
  92. }
  93.  
  94.  
  95. public synchronized void incrementCount()
  96. {
  97. int x=count+1;
  98.  
  99. try {
  100.  
  101. Thread.sleep((int) (100 + (Math.random() * (400 - 5))));
  102. } catch (InterruptedException e) {
  103. e.printStackTrace();
  104. }
  105. count= x;
  106.  
  107. }
  108.  
  109. public synchronized void decrementCount()
  110. {
  111.  
  112. count=count-1;
  113.  
  114. }
  115.  
  116.  
  117. public static void main(String[] args) throws InterruptedException {
  118. final MyClass test1 = new MyClass();
  119. final MyClass test2 = new MyClass();
  120. final MyClass test3 = new MyClass();
  121. final MyClass test4 = new MyClass();
  122. Thread t1 = new Thread() {
  123. public void run()
  124. {
  125. int k=0;
  126. while (k++<5000000)
  127. {
  128. //System.out.println("Thread 1 going to increment count");
  129. test1.incrementCount();
  130. //System.out.println("Thread 1 incremented count");
  131. }
  132. }
  133. };
  134.  
  135. Thread t2 = new Thread() {
  136. public void run()
  137. {
  138. int l=0;
  139. while (l++<5000000)
  140. {
  141. //System.out.println("Thread 1 going to increment count");
  142. test2.incrementCount();
  143. //System.out.println("Thread 2 count read = " + c);
  144. }
  145. }
  146. };
  147. Thread t3 = new Thread() {
  148. public void run()
  149. {
  150. int l=0;
  151. while (l++<5000000)
  152. {
  153. //System.out.println("Thread 1 going to increment count");
  154. test3.incrementCount();
  155. //System.out.println("Thread 2 count read = " + c);
  156. }
  157. }
  158. };
  159. Thread t4 = new Thread() {
  160. public void run()
  161. {
  162. int l=0;
  163. while (l++<5000000)
  164. {
  165. //System.out.println("Thread 1 going to increment count");
  166. test4.incrementCount();
  167. //System.out.println("Thread 2 count read = " + c);
  168. }
  169. }
  170. };
  171. // Thread t2 = new Thread() { public void run() { test1.testMethod2(); } };
  172. t1.start();
  173.  
  174. t2.start();
  175. t3.start();
  176. t4.start();
  177. t1.join();
  178. t2.join();
  179. t3.join();
  180. t4.join();
  181. //System.out.println(t2.getState());
  182. //int x=500000000+500000000;
  183. //System.out.println(x);
  184. System.out.println("count = " + MyClass.count);
  185.  
  186. }
  187. }
  188.  
  189.  
Success #stdin #stdout 0.59s 381504KB
stdin
Standard input is empty
stdout
count = 20000000