Skip to content

Commit

Permalink
JENA-1472: Merge commit 'refs/pull/348/head' of github.com:apache/jena
Browse files Browse the repository at this point in the history
This closes #348.
  • Loading branch information
afs committed Feb 1, 2018
2 parents 6228bbc + db4a620 commit 466f165
Showing 1 changed file with 18 additions and 7 deletions.
Expand Up @@ -26,10 +26,12 @@
import org.apache.jena.fuseki.Fuseki ;
import org.apache.jena.fuseki.server.DataService ;

/** The set of currently active async tasks */
/** The set of currently active and recently completed tasks. */
public class AsyncPool
{
// Max concurrent tasks.
private static int nMaxThreads = 4 ;
// Number of finished tasks kept.
private static int MAX_FINISHED = 20 ;

// See Executors.newCachedThreadPool and Executors.newFixedThreadPool
Expand All @@ -38,12 +40,14 @@ public class AsyncPool
new LinkedBlockingQueue<Runnable>()) ;
private final Object mutex = new Object() ;
private long counter = 0 ;
private Map<String, AsyncTask> runningTasks = new LinkedHashMap<>() ;
private Map<String, AsyncTask> runningTasks = new LinkedHashMap<>() ;
private Map<String, AsyncTask> finishedTasks = new LinkedHashMap<>() ;
// Finite FIFO of finished tasks.
private LinkedList<AsyncTask> finishedTasksList = new LinkedList<>();

private static AsyncPool instance = new AsyncPool() ;
public static AsyncPool get()
{ return instance ; }

public static AsyncPool get() { return instance ; }

private AsyncPool() { }

Expand Down Expand Up @@ -78,9 +82,13 @@ public void finished(AsyncTask task) {
synchronized(mutex) {
String id = task.getTaskId() ;
runningTasks.remove(id) ;
while ( finishedTasks.size() >= MAX_FINISHED )
finishedTasks.remove(task.getTaskId()) ;
// Reduce old tasks list
while ( finishedTasksList.size() >= MAX_FINISHED ) {
AsyncTask oldTask = finishedTasksList.removeFirst();
finishedTasks.remove(oldTask.getTaskId());
}
finishedTasks.put(id, task) ;
finishedTasksList.add(task);
}
}

Expand All @@ -90,7 +98,10 @@ public AsyncTask getRunningTask(String taskId) {
}
}

/** Get for any state */
/**
* Get a task - whether running or finished. Return null for no such task and also
* retired tasks. Only a limited number of old, finished tasks are remembered.
*/
public AsyncTask getTask(String taskId) {
synchronized(mutex) {
AsyncTask task = runningTasks.get(taskId) ;
Expand Down

0 comments on commit 466f165

Please sign in to comment.