From 9820962a9a0fb0fa9a49ddf49ee551ac41a708ae Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Feb 2015 16:28:58 +0100 Subject: Switch to semaphore for signalling the mainloop to check for work --- .../org/openslx/taskmanager/main/Taskmanager.java | 24 ++++++---------------- 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'daemon/src') diff --git a/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java b/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java index f9b7752..1325dca 100644 --- a/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java +++ b/daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java @@ -5,11 +5,9 @@ import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; import org.apache.log4j.Logger; import org.openslx.taskmanager.Global; @@ -46,8 +44,7 @@ public class Taskmanager implements FinishCallback, Runnable */ private final Map instances = new ConcurrentHashMap<>(); - private final Lock workLock = new ReentrantLock(); - private final Condition doCheckForWork = workLock.newCondition(); + private final Semaphore doCheckForWork = new Semaphore( 0 ); /* * Static methods @@ -142,12 +139,7 @@ public class Taskmanager implements FinishCallback, Runnable */ protected void checkForWork() { - workLock.lock(); - try { - doCheckForWork.signalAll(); - } finally { - workLock.unlock(); - } + doCheckForWork.release(); } @Override @@ -161,12 +153,8 @@ public class Taskmanager implements FinishCallback, Runnable { try { while ( !Global.doShutdown ) { - workLock.lock(); - try { - doCheckForWork.await( 1, TimeUnit.MINUTES ); - } finally { - workLock.unlock(); - } + doCheckForWork.tryAcquire( 1, TimeUnit.MINUTES ); + doCheckForWork.drainPermits(); try { for ( Iterator it = instances.values().iterator(); it.hasNext(); ) { AbstractTask task = it.next(); @@ -176,8 +164,8 @@ public class Taskmanager implements FinishCallback, Runnable continue; } if ( task.canStart() ) { - threadPool.execute( task ); log.debug( "Started Task " + task.getClass().getSimpleName() + " (" + task.getId() + ")" ); + threadPool.execute( task ); } } } catch ( RejectedExecutionException e ) { -- cgit v1.2.3-55-g7522