Guest User

Untitled

a guest
Jan 20th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. class MyInteger
  2. {
  3. private int value;
  4.  
  5. public synchronized int getValue()
  6. {
  7. return value;
  8. }
  9.  
  10. public synchronized void setValue(int value)
  11. {
  12. this.value = value;
  13. }
  14. }
  15.  
  16. public class StrangeLoop extends Thread
  17. {
  18. private MyInteger data;
  19.  
  20. public StrangeLoop(MyInteger data)
  21. {
  22. this.data = data;
  23. }
  24.  
  25. public void run()
  26. {
  27. for (int i = 1; i <= 10; i++)
  28. {
  29. int local = data.getValue();
  30. local++;
  31. data.setValue(local);
  32. }
  33. }
  34.  
  35. public static void main(String[] args)
  36. {
  37. MyInteger data = new MyInteger();
  38. StrangeLoop t1 = new StrangeLoop(data);
  39. StrangeLoop t2 = new StrangeLoop(data);
  40. t1.start();
  41. t2.start();
  42. try
  43. {
  44. t1.join();
  45. t2.join();
  46. }
  47. catch (InterruptedException e)
  48. {
  49. }
  50. System.out.println("Final value: " + data.getValue());
  51. }
  52. }
Add Comment
Please, Sign In to add comment