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 Counter {
  9. private int count = 0;
  10.  
  11. public void increment() {
  12. count++; // Thread-safe increment
  13. }
  14.  
  15. public int getCount() {
  16. return count;
  17. }
  18. }
  19. class Ideone
  20. {
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23. // your code goes here
  24. Counter c= new Counter();
  25. Thread t1 = new Thread(new Runnable(){
  26. @Override
  27. public void run(){
  28. for(int i=0;i<1000;i++){
  29. c.increment();
  30. }
  31. }
  32. }
  33. );
  34. Thread t2 = new Thread(new Runnable(){
  35. @Override
  36. public void run(){
  37. for(int i=0;i<1000;i++){
  38. c.increment();
  39. }
  40. }
  41. }
  42. );
  43. t1.start();
  44. t2.start();
  45.  
  46. t1.join();
  47. t2.join();
  48. System.out.println(c.getCount());
  49. }
  50. }
Success #stdin #stdout 0.09s 52560KB
stdin
Standard input is empty
stdout
2000