|
7 | 7 | import java.util.concurrent.Future; |
8 | 8 | import java.util.concurrent.ScheduledExecutorService; |
9 | 9 | import java.util.concurrent.TimeUnit; |
| 10 | +import java.util.concurrent.locks.Condition; |
| 11 | +import java.util.concurrent.locks.ReentrantLock; |
10 | 12 | import java.util.stream.Collectors; |
11 | 13 |
|
12 | 14 | import org.slf4j.Logger; |
@@ -52,7 +54,8 @@ public interface Handler { |
52 | 54 | private final ScheduledExecutorService scheduler; |
53 | 55 | private final String discoveryDatabase; |
54 | 56 | private final Duration discoveryTimeout; |
55 | | - private final Object readyObj = new Object(); |
| 57 | + private final ReentrantLock readyLock = new ReentrantLock(); |
| 58 | + private final Condition readyCondition = readyLock.newCondition(); |
56 | 59 |
|
57 | 60 | private volatile Instant lastUpdateTime; |
58 | 61 | private volatile Future<?> currentSchedule = null; |
@@ -90,18 +93,20 @@ public void waitReady(long millis) throws IllegalStateException { |
90 | 93 | return; |
91 | 94 | } |
92 | 95 |
|
93 | | - synchronized (readyObj) { |
94 | | - try { |
95 | | - if (isStarted) { |
96 | | - return; |
97 | | - } |
98 | | - |
99 | | - long timeout = millis > 0 ? millis : discoveryTimeout.toMillis(); |
100 | | - readyObj.wait(timeout); |
101 | | - } catch (InterruptedException ex) { |
102 | | - Thread.currentThread().interrupt(); |
103 | | - lastException = new IllegalStateException("Discovery waiting interrupted", ex); |
| 96 | + readyLock.lock(); |
| 97 | + |
| 98 | + try { |
| 99 | + if (isStarted) { |
| 100 | + return; |
104 | 101 | } |
| 102 | + |
| 103 | + long timeout = millis > 0 ? millis : discoveryTimeout.toMillis(); |
| 104 | + readyCondition.await(timeout, TimeUnit.MILLISECONDS); |
| 105 | + } catch (InterruptedException ex) { |
| 106 | + Thread.currentThread().interrupt(); |
| 107 | + lastException = new IllegalStateException("Discovery waiting interrupted", ex); |
| 108 | + } finally { |
| 109 | + readyLock.unlock(); |
105 | 110 | } |
106 | 111 |
|
107 | 112 | if (!isStarted) { |
@@ -170,19 +175,27 @@ private void runDiscovery() { |
170 | 175 | } |
171 | 176 |
|
172 | 177 | private void handleThrowable(Throwable th) { |
173 | | - synchronized (readyObj) { |
| 178 | + readyLock.lock(); |
| 179 | + |
| 180 | + try { |
174 | 181 | lastException = th; |
175 | 182 | scheduleNextTick(); |
176 | | - readyObj.notifyAll(); |
| 183 | + readyCondition.signalAll(); |
| 184 | + } finally { |
| 185 | + readyLock.unlock(); |
177 | 186 | } |
178 | 187 | } |
179 | 188 |
|
180 | 189 | private void handleOk(String selfLocation, List<EndpointRecord> endpoints) { |
181 | | - synchronized (readyObj) { |
| 190 | + readyLock.lock(); |
| 191 | + |
| 192 | + try { |
182 | 193 | isStarted = true; |
183 | 194 | lastException = null; |
184 | 195 | handler.handleEndpoints(endpoints, selfLocation).whenComplete((res, th) -> scheduleNextTick()); |
185 | | - readyObj.notifyAll(); |
| 196 | + readyCondition.signalAll(); |
| 197 | + } finally { |
| 198 | + readyLock.unlock(); |
186 | 199 | } |
187 | 200 | } |
188 | 201 |
|
|
0 commit comments