summaryrefslogtreecommitdiffstats
path: root/daemon/src
diff options
context:
space:
mode:
authorSimon Rettberg2015-02-24 16:28:58 +0100
committerSimon Rettberg2015-02-24 16:28:58 +0100
commit9820962a9a0fb0fa9a49ddf49ee551ac41a708ae (patch)
tree551b7d4ddc7b521d721ac54d532c5f123b7d9c2e /daemon/src
parentMake taskmanager an instance, add callback for finished jobs, improve parent ... (diff)
downloadtaskman-lite-9820962a9a0fb0fa9a49ddf49ee551ac41a708ae.tar.gz
taskman-lite-9820962a9a0fb0fa9a49ddf49ee551ac41a708ae.tar.xz
taskman-lite-9820962a9a0fb0fa9a49ddf49ee551ac41a708ae.zip
Switch to semaphore for signalling the mainloop to check for work
Diffstat (limited to 'daemon/src')
-rw-r--r--daemon/src/main/java/org/openslx/taskmanager/main/Taskmanager.java24
1 files changed, 6 insertions, 18 deletions
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<String, AbstractTask> 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<AbstractTask> 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 ) {