From 7321da5817968225f530e16618be61de9c1a64d7 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 20 Apr 2016 06:41:07 +0000 Subject: [PATCH] Refactor FloodLimit --- source/net/filebot/web/FloodLimit.java | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/source/net/filebot/web/FloodLimit.java b/source/net/filebot/web/FloodLimit.java index e0501257..eacb0cac 100644 --- a/source/net/filebot/web/FloodLimit.java +++ b/source/net/filebot/web/FloodLimit.java @@ -1,3 +1,4 @@ + package net.filebot.web; import java.util.concurrent.ScheduledFuture; @@ -5,10 +6,13 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import net.filebot.util.DefaultThreadFactory; + public class FloodLimit { + private static final ScheduledThreadPoolExecutor TIMER = new ScheduledThreadPoolExecutor(1, new DefaultThreadFactory("FloodLimitTimer", Thread.NORM_PRIORITY, true)); + private final Semaphore permits; - private final ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1); private final long releaseDelay; private final TimeUnit timeUnit; @@ -21,20 +25,7 @@ public class FloodLimit { public ScheduledFuture acquirePermit() throws InterruptedException { permits.acquire(); - return timer.schedule(new ReleasePermit(), releaseDelay, timeUnit); + return TIMER.schedule(permits::release, releaseDelay, timeUnit); } - public void releaseNow(ScheduledFuture future) { - if (future.cancel(false)) { - permits.release(); - } - } - - private class ReleasePermit implements Runnable { - - @Override - public void run() { - permits.release(); - } - } }