Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
[executor] Purge the thread pool periodically.
Browse files Browse the repository at this point in the history
The choice was to launch a purging task periodically.
The period is given by a system property (by default 30 seconds).

see #82

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 27, 2014
1 parent 10373af commit 5da178f
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ public final class JanusConfig {
*/
public static final String INJECTION_MODULE_NAME_VALUE = StandardJanusPlatformModule.class.getName();

/** Name of the property that contains the numbers of seconds between two purges of
* the terminated threads by the kernel.
* @see #KERNEL_THREAD_PURGE_DELAY_VALUE
*/
public static final String KERNEL_THREAD_PURGE_DELAY_NAME = "janus.executors.purgeDelay"; //$NON-NLS-1$

/** Indicates the numbers of seconds between two purges of
* the terminated threads by the kernel.
* @see #KERNEL_THREAD_PURGE_DELAY_NAME
*/
public static final int KERNEL_THREAD_PURGE_DELAY_VALUE = 30;

private JanusConfig() {
//
}
Expand All @@ -185,6 +197,7 @@ public static void getDefaultValues(Properties defaultValues) {
defaultValues.put(MAX_NUMBER_OF_THREADS_IN_EXECUTOR_NAME,
Integer.toString(MAX_NUMBER_OF_THREADS_IN_EXECUTOR_VALUE));
defaultValues.put(KERNEL_THREAD_TIMEOUT_NAME, Integer.toString(KERNEL_THREAD_TIMEOUT_VALUE));
defaultValues.put(KERNEL_THREAD_PURGE_DELAY_NAME, Integer.toString(KERNEL_THREAD_PURGE_DELAY_VALUE));
defaultValues.put(INJECTION_MODULE_NAME, INJECTION_MODULE_NAME_VALUE);
defaultValues.put(JANUS_LOGO_SHOW_NAME, JANUS_LOGO_SHOW.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void run() {
@Override
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setName("Janus shutdown"); //$NON-NLS-1$
t.setName("Janus kernel shutdown"); //$NON-NLS-1$
t.setDaemon(false);
t.setUncaughtExceptionHandler(this);
return t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.Service;
Expand All @@ -45,18 +46,35 @@
@Singleton
public class JdkExecutorService extends AbstractDependentService implements io.janusproject.services.executor.ExecutorService {

@Inject
private ScheduledExecutorService schedules;

@Inject
private ExecutorService exec;

private ScheduledFuture<?> purgeTask;

/**
*/
public JdkExecutorService() {
//
}

/** Change the JRE service for scheduled tasks.
*
* @param service - the JRE service.
*/
@Inject
void setScheduledExecutorService(ScheduledExecutorService service) {
this.schedules = service;
}

/** Change the JRE service for scheduled tasks.
*
* @param service - the JRE service.
*/
@Inject
void setExecutorService(ExecutorService service) {
this.exec = service;
}

@Override
public final Class<? extends Service> getServiceType() {
return io.janusproject.services.executor.ExecutorService.class;
Expand All @@ -66,13 +84,29 @@ public final Class<? extends Service> getServiceType() {
*/
@Override
protected void doStart() {
assert (this.schedules != null);
assert (this.exec != null);
// Launch a periodic task that is purging the executor pools.
if ((this.schedules instanceof ThreadPoolExecutor)
|| (this.exec instanceof ThreadPoolExecutor)) {
int delay = JanusConfig.getSystemPropertyAsInteger(
JanusConfig.KERNEL_THREAD_PURGE_DELAY_NAME,
JanusConfig.KERNEL_THREAD_PURGE_DELAY_VALUE);
this.purgeTask = this.schedules.scheduleWithFixedDelay(
new Purger(),
delay, delay, TimeUnit.SECONDS);
}
notifyStarted();
}

/** {@inheritDoc}
*/
@Override
protected void doStop() {
if (this.purgeTask != null) {
this.purgeTask.cancel(true);
this.purgeTask = null;
}
this.exec.shutdown();
this.schedules.shutdown();
try {
Expand Down Expand Up @@ -152,4 +186,74 @@ public ExecutorService getExecutorService() {
return this.exec;
}

/** {@inheritDoc}
*/
@Override
public void purge() {
if (this.exec instanceof ThreadPoolExecutor) {
((ThreadPoolExecutor) this.exec).purge();
}
if (this.schedules instanceof ThreadPoolExecutor) {
((ThreadPoolExecutor) this.schedules).purge();
}
}

/** Task that is purging the thread pools.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
private class Purger implements Runnable {

private String oldThreadName;

/**
*/
public Purger() {
//
}

private boolean setName() {
if (this.oldThreadName != null) {
return false;
}
Thread t = Thread.currentThread();
this.oldThreadName = t.getName();
t.setName(toString());
return true;
}

private boolean restoreName() {
if (this.oldThreadName == null) {
return false;
}
Thread t = Thread.currentThread();
t.setName(this.oldThreadName);
this.oldThreadName = null;
return true;
}

/** {@inheritDoc}
*/
@Override
public void run() {
assert (setName());
try {
purge();
} finally {
assert (restoreName());
}
}

/** {@inheritDoc}
*/
@Override
public String toString() {
return "Janus Thread Purger"; //$NON-NLS-1$
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay,
long delay, TimeUnit unit);

/** Remove any canceled/terminated tasks from the lists of tasks.
*/
void purge();

}
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ public void doStop() {
Mockito.verify(this.scheduledExecutorService, new Times(1)).shutdownNow();
Mockito.verify(this.executorService, new Times(1)).shutdownNow();
}

}

0 comments on commit 5da178f

Please sign in to comment.