Introduced capability of configuring the daemon property on schedulers.

Also added the name property to ScheduledExecutorScheduler.
This commit is contained in:
Simone Bordet 2013-01-10 10:26:04 +01:00
parent 34cc0e5d82
commit f096380a35
2 changed files with 37 additions and 4 deletions

View File

@ -20,18 +20,49 @@ package org.eclipse.jetty.util.thread;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
/**
* Implementation of {@link Scheduler} based on JDK's {@link ScheduledThreadPoolExecutor}.
* <p />
* While use of {@link ScheduledThreadPoolExecutor} creates futures that will not be used,
* it has the advantage of allowing to set a property to remove cancelled tasks from its
* queue even if the task did not fire, which provides a huge benefit in the performance
* of garbage collection in young generation.
*/
public class ScheduledExecutorScheduler extends AbstractLifeCycle implements Scheduler
{
private final String name;
private final boolean daemon;
private volatile ScheduledThreadPoolExecutor scheduler;
public ScheduledExecutorScheduler()
{
this(null, false);
}
public ScheduledExecutorScheduler(String name, boolean daemon)
{
this.name = name == null ? "Scheduler-" + hashCode() : name;
this.daemon = daemon;
}
@Override
protected void doStart() throws Exception
{
scheduler = new ScheduledThreadPoolExecutor(1);
scheduler = new ScheduledThreadPoolExecutor(1, new ThreadFactory()
{
@Override
public Thread newThread(Runnable r)
{
Thread thread = new Thread(r, name);
thread.setDaemon(daemon);
return thread;
}
});
scheduler.setRemoveOnCancelPolicy(true);
super.doStart();
}

View File

@ -39,22 +39,24 @@ public class TimerScheduler extends AbstractLifeCycle implements Scheduler
*/
private final String _name;
private final boolean _daemon;
private Timer _timer;
public TimerScheduler()
{
this(null);
this(null, false);
}
public TimerScheduler(String name)
public TimerScheduler(String name, boolean daemon)
{
_name=name;
_daemon=daemon;
}
@Override
protected void doStart() throws Exception
{
_timer=_name==null?new Timer():new Timer(_name);
_timer=_name==null?new Timer():new Timer(_name,_daemon);
super.doStart();
}