1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util;
20
21 import java.util.concurrent.atomic.AtomicInteger;
22 import java.util.concurrent.atomic.AtomicLong;
23
24 public class Atomics
25 {
26 private Atomics()
27 {
28 }
29
30 public static boolean updateMin(AtomicLong currentMin, long newValue)
31 {
32 long oldValue = currentMin.get();
33 while (newValue < oldValue)
34 {
35 if (currentMin.compareAndSet(oldValue, newValue))
36 return true;
37 oldValue = currentMin.get();
38 }
39 return false;
40 }
41
42 public static boolean updateMax(AtomicLong currentMax, long newValue)
43 {
44 long oldValue = currentMax.get();
45 while (newValue > oldValue)
46 {
47 if (currentMax.compareAndSet(oldValue, newValue))
48 return true;
49 oldValue = currentMax.get();
50 }
51 return false;
52 }
53
54 public static boolean updateMin(AtomicInteger currentMin, int newValue)
55 {
56 int oldValue = currentMin.get();
57 while (newValue < oldValue)
58 {
59 if (currentMin.compareAndSet(oldValue, newValue))
60 return true;
61 oldValue = currentMin.get();
62 }
63 return false;
64 }
65
66 public static boolean updateMax(AtomicInteger currentMax, int newValue)
67 {
68 int oldValue = currentMax.get();
69 while (newValue > oldValue)
70 {
71 if (currentMax.compareAndSet(oldValue, newValue))
72 return true;
73 oldValue = currentMax.get();
74 }
75 return false;
76 }
77 }