Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. class counterThreadClash
  2. {
  3.  
  4. static Counter count = new Counter();
  5.  
  6. static class t1 extends Thread
  7. {
  8. public void run()
  9. {
  10. for (int i=0;i<50000;i++) {
  11. count.increment();
  12. }
  13. System.out.println(count.value());
  14. }
  15.  
  16. }
  17.  
  18. static class t2 extends Thread
  19. {
  20. public void run()
  21. {
  22. for (int i=0;i<50000;i++) {
  23. count.increment();
  24. }
  25. System.out.println(count.value());
  26. }
  27. }
  28.  
  29. public static void main(String [] args)
  30. {
  31. new t1().start();
  32. new t2().start();
  33. }
  34. }
  35.  
  36. class Counter {
  37. private int c = 0;
  38.  
  39. public void increment() {
  40. c++;
  41. }
  42.  
  43. public void decrement() {
  44. c--;
  45. }
  46.  
  47. public int value() {
  48. return c;
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement