Committed by
Gerrit Code Review
Refactor: Use CompletableFuture instead of Future
Change-Id: I7bf71a31a6c870c59783caa638c47bc040f655c8
Showing
1 changed file
with
7 additions
and
7 deletions
| ... | @@ -51,9 +51,9 @@ import java.util.EnumSet; | ... | @@ -51,9 +51,9 @@ import java.util.EnumSet; |
| 51 | import java.util.List; | 51 | import java.util.List; |
| 52 | import java.util.Map; | 52 | import java.util.Map; |
| 53 | import java.util.Optional; | 53 | import java.util.Optional; |
| 54 | +import java.util.concurrent.CompletableFuture; | ||
| 54 | import java.util.concurrent.ExecutionException; | 55 | import java.util.concurrent.ExecutionException; |
| 55 | import java.util.concurrent.ExecutorService; | 56 | import java.util.concurrent.ExecutorService; |
| 56 | -import java.util.concurrent.Future; | ||
| 57 | import java.util.stream.Collectors; | 57 | import java.util.stream.Collectors; |
| 58 | 58 | ||
| 59 | import static com.google.common.base.Preconditions.checkNotNull; | 59 | import static com.google.common.base.Preconditions.checkNotNull; |
| ... | @@ -286,10 +286,10 @@ public class IntentManager | ... | @@ -286,10 +286,10 @@ public class IntentManager |
| 286 | } | 286 | } |
| 287 | } | 287 | } |
| 288 | 288 | ||
| 289 | - private Future<FinalIntentProcessPhase> submitIntentData(IntentData data) { | 289 | + private CompletableFuture<FinalIntentProcessPhase> submitIntentData(IntentData data) { |
| 290 | IntentData current = store.getIntentData(data.key()); | 290 | IntentData current = store.getIntentData(data.key()); |
| 291 | IntentProcessPhase initial = newInitialPhase(processor, data, current); | 291 | IntentProcessPhase initial = newInitialPhase(processor, data, current); |
| 292 | - return workerExecutor.submit(() -> { | 292 | + return CompletableFuture.supplyAsync(() -> { |
| 293 | Optional<IntentProcessPhase> currentPhase = Optional.of(initial); | 293 | Optional<IntentProcessPhase> currentPhase = Optional.of(initial); |
| 294 | IntentProcessPhase previousPhase = initial; | 294 | IntentProcessPhase previousPhase = initial; |
| 295 | 295 | ||
| ... | @@ -298,7 +298,7 @@ public class IntentManager | ... | @@ -298,7 +298,7 @@ public class IntentManager |
| 298 | currentPhase = previousPhase.execute(); | 298 | currentPhase = previousPhase.execute(); |
| 299 | } | 299 | } |
| 300 | return (FinalIntentProcessPhase) previousPhase; | 300 | return (FinalIntentProcessPhase) previousPhase; |
| 301 | - }); | 301 | + }, workerExecutor); |
| 302 | } | 302 | } |
| 303 | 303 | ||
| 304 | private class InternalBatchDelegate implements IntentBatchDelegate { | 304 | private class InternalBatchDelegate implements IntentBatchDelegate { |
| ... | @@ -333,15 +333,15 @@ public class IntentManager | ... | @@ -333,15 +333,15 @@ public class IntentManager |
| 333 | } | 333 | } |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | - private List<Future<FinalIntentProcessPhase>> createIntentUpdates(Collection<IntentData> data) { | 336 | + private List<CompletableFuture<FinalIntentProcessPhase>> createIntentUpdates(Collection<IntentData> data) { |
| 337 | return data.stream() | 337 | return data.stream() |
| 338 | .map(IntentManager.this::submitIntentData) | 338 | .map(IntentManager.this::submitIntentData) |
| 339 | .collect(Collectors.toList()); | 339 | .collect(Collectors.toList()); |
| 340 | } | 340 | } |
| 341 | 341 | ||
| 342 | - private List<FinalIntentProcessPhase> waitForFutures(List<Future<FinalIntentProcessPhase>> futures) { | 342 | + private List<FinalIntentProcessPhase> waitForFutures(List<CompletableFuture<FinalIntentProcessPhase>> futures) { |
| 343 | ImmutableList.Builder<FinalIntentProcessPhase> updateBuilder = ImmutableList.builder(); | 343 | ImmutableList.Builder<FinalIntentProcessPhase> updateBuilder = ImmutableList.builder(); |
| 344 | - for (Future<FinalIntentProcessPhase> future : futures) { | 344 | + for (CompletableFuture<FinalIntentProcessPhase> future : futures) { |
| 345 | try { | 345 | try { |
| 346 | updateBuilder.add(future.get()); | 346 | updateBuilder.add(future.get()); |
| 347 | } catch (InterruptedException | ExecutionException e) { | 347 | } catch (InterruptedException | ExecutionException e) { | ... | ... |
-
Please register or login to post a comment