Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. public class Counter implements Comparable<Counter> {
  2. private volatile long value;
  3.  
  4. public long getValue() {
  5. return value;
  6. }
  7.  
  8. public synchronized void reset() {
  9. value = 0;
  10. notifyAll();
  11. }
  12.  
  13. public synchronized void incr() {
  14. value++;
  15. notifyAll();
  16. }
  17.  
  18. public synchronized void decr() {
  19. value--;
  20. notifyAll();
  21. }
  22.  
  23. @Override
  24. public int hashCode() {
  25. return 17 + (int) (this.value ^ (this.value >>> 32));
  26. }
  27.  
  28. @Override
  29. public boolean equals(Object o) {
  30. return o instanceof Counter && ((Counter)o).value == value;
  31. }
  32.  
  33. @Override
  34. public int compareTo(Counter o) {
  35. if( o.value > value )
  36. return -1;
  37. else if( o.value < value )
  38. return 1;
  39. else
  40. return 0;
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement