org.eclipse.jetty.util.thread.TimerScheduler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jetty-util Show documentation
Show all versions of jetty-util Show documentation
Utility classes for Jetty
//
// ========================================================================
// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.thread;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
public class TimerScheduler extends AbstractLifeCycle implements Scheduler
{
/* 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
* Futures involved (which we do not need).
* However, Timer is still locking and a concurrent queue would be better.
*/
Timer _timer;
final String _name;
public TimerScheduler()
{
this(null);
}
public TimerScheduler(String name)
{
_name=name;
}
@Override
protected void doStart() throws Exception
{
_timer=_name==null?new Timer():new Timer(_name);
super.doStart();
}
@Override
protected void doStop() throws Exception
{
_timer.cancel();
super.doStop();
_timer=null;
}
@Override
public Task schedule(final Runnable task, final long delay, final TimeUnit units)
{
Timer timer=_timer;
if (timer!=null)
{
SimpleTask t = new SimpleTask(task);
_timer.schedule(t,units.toMillis(delay));
return t;
}
throw new IllegalStateException("STOPPED: "+this);
}
private static class SimpleTask extends TimerTask implements Task
{
private final Runnable _task;
SimpleTask(Runnable runnable)
{
_task=runnable;
}
@Override
public void run()
{
_task.run();
}
}
}