From 23d98684e460cca2ce1ca474b965af7592db3e9d Mon Sep 17 00:00:00 2001 From: Martin Derka Date: Fri, 4 Mar 2016 09:41:11 -0800 Subject: [PATCH] Added pool of executors instead of spawning one threads. --- .../gcloud/dns/testing/LocalDnsHelper.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java index 984d58ef496b..cafbf764db69 100644 --- a/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java +++ b/gcloud-java-dns/src/main/java/com/google/gcloud/dns/testing/LocalDnsHelper.java @@ -69,6 +69,9 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; @@ -106,8 +109,11 @@ public class LocalDnsHelper { private static final TreeSet FORBIDDEN = Sets.newTreeSet( ImmutableList.of("google.com.", "com.", "example.com.", "net.", "org.")); private static final Pattern ZONE_NAME_RE = Pattern.compile("[a-z][a-z0-9-]*"); + private static final ScheduledExecutorService EXECUTORS = + Executors.newScheduledThreadPool(4, Executors.defaultThreadFactory()); static { + try { BASE_CONTEXT = new URI(CONTEXT); } catch (URISyntaxException e) { @@ -741,28 +747,20 @@ Response createChange(String projectId, String zoneName, Change change, String.. } /** - * Applies change. Uses a new thread which applies the change only if DELAY_CHANGE is > 0. + * Applies change. Uses a different pooled thread which applies the change only if {@code + * delayChange} is > 0. */ - private Thread invokeChange(final String projectId, final String zoneName, + private void invokeChange(final String projectId, final String zoneName, final String changeId) { if (delayChange > 0) { - Thread thread = new Thread() { + EXECUTORS.schedule(new Runnable() { @Override public void run() { - try { - Thread.sleep(delayChange); // simulate delayed execution - } catch (InterruptedException ex) { - log.log(Level.WARNING, "Thread was interrupted while sleeping.", ex); - } - // start applying the changes applyExistingChange(projectId, zoneName, changeId); } - }; - thread.start(); - return thread; + }, delayChange, TimeUnit.MILLISECONDS); } else { applyExistingChange(projectId, zoneName, changeId); - return null; } }