Skip to content

Commit 2942239

Browse files
committed
dev: Recode async method
1 parent 322901e commit 2942239

File tree

1 file changed

+2
-7
lines changed

1 file changed

+2
-7
lines changed

core/src/main/java/dev/dotspace/common/SpaceCollections.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
import java.util.Collection;
99
import java.util.Optional;
1010
import java.util.concurrent.CompletableFuture;
11-
import java.util.concurrent.ExecutorService;
12-
import java.util.concurrent.Executors;
1311
import java.util.concurrent.ThreadLocalRandom;
1412

1513
/**
1614
* Class with {@link Collection} operations
1715
*/
1816
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1917
public final class SpaceCollections {
20-
private final static ExecutorService SERVICE = Executors.newCachedThreadPool();
21-
2218
/**
2319
* Get a random object of collection. Object wrapped in {@link Optional}
2420
*
@@ -46,8 +42,7 @@ public final class SpaceCollections {
4642
* or empty or if the given object is null in list.
4743
*/
4844
public static <T> @NotNull CompletableFuture<@Nullable T> asyncRandom(final Collection<T> collection) {
49-
final CompletableFuture<@Nullable T> completableFuture = new CompletableFuture<>(); //Create new completableFuture
50-
SERVICE.execute(() -> completableFuture.complete(SpaceCollections.random(collection).orElse(null))); //Complete the future in a separate thread
51-
return completableFuture;
45+
return new CompletableFuture<@Nullable T>()
46+
.completeAsync(() -> SpaceCollections.random(collection).orElse(null)); //Complete the future in a separate thread
5247
}
5348
}

0 commit comments

Comments
 (0)