You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ti...@apache.org on 2022/05/20 03:53:13 UTC
[curator] branch master updated: CURATOR-514: Utilize ThreadLocalRandom In QueueSharder (#307)
This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/curator.git
The following commit(s) were added to refs/heads/master by this push:
new f44ff884 CURATOR-514: Utilize ThreadLocalRandom In QueueSharder (#307)
f44ff884 is described below
commit f44ff8844f0727e155d7f8e8e6172757acc060cd
Author: belugabehr <12...@users.noreply.github.com>
AuthorDate: Thu May 19 23:53:07 2022 -0400
CURATOR-514: Utilize ThreadLocalRandom In QueueSharder (#307)
---
.../apache/curator/framework/recipes/queue/QueueSharder.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/QueueSharder.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/QueueSharder.java
index 3cd0cdbe..32470e4b 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/QueueSharder.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/queue/QueueSharder.java
@@ -35,13 +35,13 @@ import java.io.Closeable;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
-import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicReference;
/**
@@ -74,7 +74,6 @@ public class QueueSharder<U, T extends QueueBase<U>> implements Closeable
private final Set<String> preferredQueues = Sets.newSetFromMap(Maps.<String, Boolean>newConcurrentMap());
private final AtomicReference<State> state = new AtomicReference<State>(State.LATENT);
private final LeaderLatch leaderLatch;
- private final Random random = new Random();
private final ExecutorService service;
private static final String QUEUE_PREFIX = "queue-";
@@ -179,12 +178,13 @@ public class QueueSharder<U, T extends QueueBase<U>> implements Closeable
List<String> localPreferredQueues = Lists.newArrayList(preferredQueues);
if ( localPreferredQueues.size() > 0 )
{
- String key = localPreferredQueues.get(random.nextInt(localPreferredQueues.size()));
+ String key = localPreferredQueues.get(
+ ThreadLocalRandom.current().nextInt(localPreferredQueues.size()));
return queues.get(key);
}
- List<String> keys = Lists.newArrayList(queues.keySet());
- String key = keys.get(random.nextInt(keys.size()));
+ List<String> keys = Lists.newArrayList(queues.keySet());
+ String key = keys.get(ThreadLocalRandom.current().nextInt(keys.size()));
return queues.get(key);
}