Cleaned up implementation.

This commit is contained in:
Simone Bordet 2012-10-25 16:34:38 +02:00
parent e2a988f8fc
commit 31b77d9ade
1 changed files with 12 additions and 13 deletions

View File

@ -20,19 +20,22 @@ package org.eclipse.jetty.util.thread;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.component.AbstractLifeCycle; import org.eclipse.jetty.util.component.AbstractLifeCycle;
public class TimerScheduler extends AbstractLifeCycle implements Scheduler public class TimerScheduler extends AbstractLifeCycle implements Scheduler
{ {
/* this class uses the Timer class rather than an ScheduledExecutionService because /*
* This class uses the Timer class rather than an ScheduledExecutionService because
* it uses the same algorithm internally and the signature is cheaper to use as there are no * it uses the same algorithm internally and the signature is cheaper to use as there are no
* Futures involved (which we do not need). * Futures involved (which we do not need).
* However, Timer is still locking and a concurrent queue would be better. * However, Timer is still locking and a concurrent queue would be better.
*/ */
Timer _timer;
final String _name; private final String _name;
private Timer _timer;
public TimerScheduler() public TimerScheduler()
{ {
@ -63,20 +66,18 @@ public class TimerScheduler extends AbstractLifeCycle implements Scheduler
public Task schedule(final Runnable task, final long delay, final TimeUnit units) public Task schedule(final Runnable task, final long delay, final TimeUnit units)
{ {
Timer timer=_timer; Timer timer=_timer;
if (timer!=null) if (timer==null)
{ throw new RejectedExecutionException("STOPPED: "+this);
SimpleTask t = new SimpleTask(task); SimpleTask t = new SimpleTask(task);
_timer.schedule(t,units.toMillis(delay)); timer.schedule(t,units.toMillis(delay));
return t; return t;
}
throw new IllegalStateException("STOPPED: "+this);
} }
private static class SimpleTask extends TimerTask implements Task private static class SimpleTask extends TimerTask implements Task
{ {
private final Runnable _task; private final Runnable _task;
SimpleTask(Runnable runnable) private SimpleTask(Runnable runnable)
{ {
_task=runnable; _task=runnable;
} }
@ -87,6 +88,4 @@ public class TimerScheduler extends AbstractLifeCycle implements Scheduler
_task.run(); _task.run();
} }
} }
} }