Java/Development Class/StopWatch — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:01, 1 июня 2010
Содержание
- 1 Basic Timer
- 2 Provides the programatic analog of a physical stop watch
- 3 Provides various features related to the current date and time
- 4 Simple stop watch
- 5 Simulates a stop watch with a lap counter.
- 6 Some simple stop watch.
- 7 Stop Watch from JGraphT
- 8 Stopwatch is a debugging tool for manually profiling code execution
- 9 StopWatch provides a convenient API for timings.
- 10 StopWatch reports elapsed time between start and stop
- 11 Stop watch with nano second
- 12 Stop Watch with PrintWriter
- 13 Your own timer
Basic Timer
import java.util.*;
/**
* @author Matthew D. Hicks
*
*/
public class BasicTimer {
public static final int TYPE_CLOCK = 1;
public static final int TYPE_ELAPSED = 2;
public static final int TYPE_REMAINING = 3;
private int type;
private long start;
private long max;
public BasicTimer(int type) {
this.type = type;
start = System.currentTimeMillis();
}
public long getTime() {
if (type == TYPE_CLOCK) {
return System.currentTimeMillis();
} else if (type == TYPE_ELAPSED) {
return System.currentTimeMillis() - start;
} else if (type == TYPE_REMAINING) {
return max - (System.currentTimeMillis() - start);
}
throw new RuntimeException("Type does not match anything known: " + type);
}
public void setMax(long max) {
this.max = max;
}
public static void main(String[] args) throws Exception {
int type = TYPE_REMAINING;
BasicTimer timer = new BasicTimer(type);
timer.setMax(10000);
GregorianCalendar calendar = new GregorianCalendar();
while (true) {
Thread.sleep(1000);
if (type == TYPE_CLOCK) {
calendar.setTimeInMillis(timer.getTime());
System.out.println("Clock: " + calendar);
} else if (type == TYPE_ELAPSED) {
System.out.println("Elapsed: " + (timer.getTime() / 1000) + " seconds");
} else if (type == TYPE_REMAINING) {
System.out.println("Remaining: " + (timer.getTime() / 1000) + " seconds");
}
}
}
}
Provides the programatic analog of a physical stop watch
/* Copyright (c) 2001-2009, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Provides the programatic analog of a physical stop watch. <p>
*
* The watch can be started, stopped and zeroed and can be queried for
* elapsed running time. The watch accumulates elapsed time over starts
* and stops such that only the time actually spent running is recorded.
* If the watch is zeroed, then the accumulated time is discarded and
* the watch starts again with zero acumulated time. <p>
*
* @author boucherb@users
* @version 1.7.2
* @since 1.7.2
*/
public class StopWatch {
/**
* The last time this object made the transition
* from stopped to running state, as reported
* by System.currentTimeMillis().
*/
private long startTime;
private long lastStart;
/**
* The accumulated running time of this object since
* it was last zeroed.
*/
private long total;
/** Flags if this object is started or stopped. */
boolean running = false;
/** Creates, zeros, and starts a new StopWatch */
public StopWatch() {
this(true);
}
/** Creates, zeros, and starts a new StopWatch */
public StopWatch(boolean start) {
if (start) {
start();
}
}
/**
* Retrieves the accumulated time this object has spent running since
* it was last zeroed.
* @return the accumulated time this object has spent running since
* it was last zeroed.
*/
public long elapsedTime() {
if (running) {
return total + System.currentTimeMillis() - startTime;
} else {
return total;
}
}
/**
* Retrieves the accumulated time this object has spent running since
* it was last started.
* @return the accumulated time this object has spent running since
* it was last started.
*/
public long currentElapsedTime() {
if (running) {
return System.currentTimeMillis() - startTime;
} else {
return 0;
}
}
/** Zeros accumulated running time and restarts this object. */
public void zero() {
total = 0;
start();
}
/**
* Ensures that this object is in the running state. If this object is not
* running, then the call has the effect of setting the <code>startTime</code>
* attribute to the current value of System.currentTimeMillis() and setting
* the <code>running</code> attribute to <code>true</code>.
*/
public void start() {
startTime = System.currentTimeMillis();
running = true;
}
/**
* Ensures that this object is in the stopped state. If this object is
* in the running state, then this has the effect of adding to the
* <code>total</code> attribute the elapsed time since the last transition
* from stopped to running state and sets the <code>running</code> attribute
* to false. If this object is not in the running state, this call has no
* effect.
*/
public void stop() {
if (running) {
total += System.currentTimeMillis() - startTime;
running = false;
}
}
public void mark() {
stop();
start();
}
/**
* Retrieves prefix + " in " + elapsedTime() + " ms."
* @param prefix The string to use as a prefix
* @return prefix + " in " + elapsedTime() + " ms."
*/
public String elapsedTimeToMessage(String prefix) {
return prefix + " in " + elapsedTime() + " ms.";
}
/**
* Retrieves prefix + " in " + elapsedTime() + " ms."
* @param prefix The string to use as a prefix
* @return prefix + " in " + elapsedTime() + " ms."
*/
public String currentElapsedTimeToMessage(String prefix) {
return prefix + " in " + currentElapsedTime() + " ms.";
}
/**
* Retrieves the internal state of this object, as a String.
*
* The retreived value is:
*
* <pre>
* super.toString() +
* "[running=" +
* running +
* ", startTime=" +
* startTime +
* ", total=" +
* total + "]";
* </pre>
* @return the state of this object, as a String
*/
public String toString() {
return super.toString() + "[running=" + running + ", startTime="
+ startTime + ", total=" + total + "]";
}
}
//----------------------------------------------------------------------------//
// //
// C l o c k //
// //
// Copyright (C) Herve Bitteur 2000-2009. All rights reserved. //
// This software is released under the GNU General Public License. //
// Please contact users@audiveris.dev.java.net to report bugs & suggestions. //
//----------------------------------------------------------------------------//
//
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
/**
* Class <code>Clock</code> provides various features related to the current
* date and time, as well as the elapsed time since the beginning of the
* application.
*
* @author Hervé Bitteur
* @version $Id: Clock.java,v 1.8 2009/03/03 19:45:52 hbitteur Exp $
*/
public class Clock
{
//~ Static fields/initializers ---------------------------------------------
/** To have a reference time */
private static long startTime = System.currentTimeMillis();
/** General date formatting */
private static DateFormat dateFormatter = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL,
Locale.US);
/** General time formatting. Locale to be used, could be: //Locale.US;
//Locale.FRANCE; */
private static Locale locale = Locale.getDefault();
/** Corresponding number format */
private static NumberFormat nf = NumberFormat.getNumberInstance(locale);
/** Decimal format */
private static DecimalFormat timeFormatter = (DecimalFormat) nf;
static {
timeFormatter.applyPattern("000,000.00");
}
//~ Constructors -----------------------------------------------------------
//-------//
// Clock // To prevent instantiation
//-------//
private Clock ()
{
}
//~ Methods ----------------------------------------------------------------
//---------//
// getDate //
//---------//
/**
* Retrieves the values of current date and time of day, and formats a
* standard string,
*
* @return A standardized date + time string
*/
public static String getDate ()
{
return dateFormatter.format(new Date());
}
//------------//
// getElapsed //
//------------//
/**
* Retrieves the number of milliseconds since the reference start time, and
* formats a standardized string using seconds and milliseconds. NB: The
* start time is usually the time when this class was elaborated. It can
* also be later reset, via the "reset" method.
*
* @return A standardized duration string
*/
public static String getElapsed ()
{
long delta = System.currentTimeMillis() - startTime;
return timeFormatter.format((double) delta / 1000);
}
//-----------//
// resetTime //
//-----------//
/**
* Resets the reference start value at the time this method is called.
*/
public static void resetTime ()
{
startTime = System.currentTimeMillis();
}
}
Simple stop watch
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
/**
* Simple stop watch, allowing for timing of a number of tasks,
* exposing total running time and running time for each named task.
*
* <p>Conceals use of <code>System.currentTimeMillis()</code>, improving the
* readability of application code and reducing the likelihood of calculation errors.
*
* <p>Note that this object is not designed to be thread-safe and does not
* use synchronization.
*
* <p>This class is normally used to verify performance during proof-of-concepts
* and in development, rather than as part of production applications.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since May 2, 2001
*/
public class StopWatch {
/**
* Identifier of this stop watch.
* Handy when we have output from multiple stop watches
* and need to distinguish between them in log or console output.
*/
private final String id;
private boolean keepTaskList = true;
/** List of TaskInfo objects */
private final List taskList = new LinkedList();
/** Start time of the current task */
private long startTimeMillis;
/** Is the stop watch currently running? */
private boolean running;
/** Name of the current task */
private String currentTaskName;
private TaskInfo lastTaskInfo;
private int taskCount;
/** Total running time */
private long totalTimeMillis;
/**
* Construct a new stop watch. Does not start any task.
*/
public StopWatch() {
this.id = "";
}
/**
* Construct a new stop watch with the given id.
* Does not start any task.
* @param id identifier for this stop watch.
* Handy when we have output from multiple stop watches
* and need to distinguish between them.
*/
public StopWatch(String id) {
this.id = id;
}
/**
* Determine whether the TaskInfo array is built over time. Set this to
* "false" when using a StopWatch for millions of intervals, or the task
* info structure will consume excessive memory. Default is "true".
*/
public void setKeepTaskList(boolean keepTaskList) {
this.keepTaskList = keepTaskList;
}
/**
* Start an unnamed task. The results are undefined if {@link #stop()}
* or timing methods are called without invoking this method.
* @see #stop()
*/
public void start() throws IllegalStateException {
start("");
}
/**
* Start a named task. The results are undefined if {@link #stop()}
* or timing methods are called without invoking this method.
* @param taskName the name of the task to start
* @see #stop()
*/
public void start(String taskName) throws IllegalStateException {
if (this.running) {
throw new IllegalStateException("Can"t start StopWatch: it"s already running");
}
this.startTimeMillis = System.currentTimeMillis();
this.running = true;
this.currentTaskName = taskName;
}
/**
* Stop the current task. The results are undefined if timing
* methods are called without invoking at least one pair
* {@link #start()} / {@link #stop()} methods.
* @see #start()
*/
public void stop() throws IllegalStateException {
if (!this.running) {
throw new IllegalStateException("Can"t stop StopWatch: it"s not running");
}
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(lastTaskInfo);
}
++this.taskCount;
this.running = false;
this.currentTaskName = null;
}
/**
* Return whether the stop watch is currently running.
*/
public boolean isRunning() {
return this.running;
}
/**
* Return the time taken by the last task.
*/
public long getLastTaskTimeMillis() throws IllegalStateException {
if (this.lastTaskInfo == null) {
throw new IllegalStateException("No tests run: can"t get last interval");
}
return this.lastTaskInfo.getTimeMillis();
}
/**
* Return the total time in milliseconds for all tasks.
*/
public long getTotalTimeMillis() {
return totalTimeMillis;
}
/**
* Return the total time in seconds for all tasks.
*/
public double getTotalTimeSeconds() {
return totalTimeMillis / 1000.0;
}
/**
* Return the number of tasks timed.
*/
public int getTaskCount() {
return taskCount;
}
/**
* Return an array of the data for tasks performed.
*/
public TaskInfo[] getTaskInfo() {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
}
return (TaskInfo[]) this.taskList.toArray(new TaskInfo[this.taskList.size()]);
}
/**
* Return a short description of the total running time.
*/
public String shortSummary() {
return "StopWatch "" + this.id + "": running time (millis) = " + getTotalTimeMillis();
}
/**
* Return a string with a table describing all tasks performed.
* For custom reporting, call getTaskInfo() and use the task info directly.
*/
public String prettyPrint() {
StringBuffer sb = new StringBuffer(shortSummary());
sb.append("\n");
if (!this.keepTaskList) {
sb.append("No task info kept");
}
else {
TaskInfo[] tasks = getTaskInfo();
sb.append("-----------------------------------------\n");
sb.append("ms % Task name\n");
sb.append("-----------------------------------------\n");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(5);
nf.setGroupingUsed(false);
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
for (int i = 0; i < tasks.length; i++) {
sb.append(nf.format(tasks[i].getTimeMillis()) + " ");
sb.append(pf.format(tasks[i].getTimeSeconds() / getTotalTimeSeconds()) + " ");
sb.append(tasks[i].getTaskName() + "\n");
}
}
return sb.toString();
}
/**
* Return an informative string describing all tasks performed
* For custom reporting, call <code>getTaskInfo()</code> and use the task info directly.
*/
public String toString() {
StringBuffer sb = new StringBuffer(shortSummary());
if (this.keepTaskList) {
TaskInfo[] tasks = getTaskInfo();
for (int i = 0; i < tasks.length; i++) {
sb.append("; [" + tasks[i].getTaskName() + "] took " + tasks[i].getTimeMillis());
long percent = Math.round((100.0 * tasks[i].getTimeSeconds()) / getTotalTimeSeconds());
sb.append(" = " + percent + "%");
}
}
else {
sb.append("; no task info kept");
}
return sb.toString();
}
/**
* Inner class to hold data about one task executed within the stop watch.
*/
public static class TaskInfo {
private final String taskName;
private final long timeMillis;
private TaskInfo(String taskName, long timeMillis) {
this.taskName = taskName;
this.timeMillis = timeMillis;
}
/**
* Return the name of this task.
*/
public String getTaskName() {
return taskName;
}
/**
* Return the time in milliseconds this task took.
*/
public long getTimeMillis() {
return timeMillis;
}
/**
* Return the time in seconds this task took.
*/
public double getTimeSeconds() {
return timeMillis / 1000.0;
}
}
}
Simulates a stop watch with a lap counter.
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
import java.io.Serializable;
/**
* Simulates a stop watch with a <em>lap</em> counter.
*
* @version <tt>$Revision: 2800 $</tt>
* @author
*/
public class StopWatch implements Serializable, Cloneable {
/** The serialVersionUID */
private static final long serialVersionUID = 4628094303187435707L;
/** Total time */
protected long total = 0;
/** Start time */
protected long start = -1;
/** Stop time */
protected long stop = -1;
/** The <i>lap</i> count */
protected int count = 0;
/** Is the watch started */
protected boolean running = false;
/**
* Default constructor.
*/
public StopWatch() {
}
/**
* Construct a StopWatch.
*
* @param running
* Start the watch
*/
public StopWatch(final boolean running) {
if (running)
start();
}
/**
* Start the watch.
*
* @param reset
* True to reset the watch prior to starting.
*/
public void start(final boolean reset) {
if (!running) {
if (reset)
reset();
start = System.currentTimeMillis();
running = true;
}
}
/**
* Start the watch.
*/
public void start() {
start(false);
}
/**
* Stop the watch.
*
* @return Elapsed time or 0 if the watch was never started.
*/
public long stop() {
long lap = 0;
if (running) {
count++;
stop = System.currentTimeMillis();
lap = stop - start;
total += lap;
running = false;
}
return lap;
}
/**
* Reset the watch.
*/
public void reset() {
start = -1;
stop = -1;
total = 0;
count = 0;
running = false;
}
/**
* Get the <i>lap</i> count.
*
* @return The <i>lap</i> count.
*/
public int getLapCount() {
return count;
}
/**
* Get the elapsed <i>lap</i> time since the watch was started.
*
* @return Elapsed <i>lap</i> time or 0 if the watch was never started
*/
public long getLapTime() {
if (start == -1) {
return 0;
} else if (running) {
return System.currentTimeMillis() - start;
} else {
return stop - start;
}
}
/**
* Get the average <i>lap</i> time since the watch was started.
*
* @return Average <i>lap</i> time since the watch was started.
*/
public long getAverageLapTime() {
return (count == 0) ? 0 : getLapTime() / getLapCount();
}
/**
* Get the elapsed time since the watch was created or last reset.
*
* @return Elapsed time or 0 if the watch was never started.
*/
public long getTime() {
if (start == -1) {
return 0;
} else if (running) {
return total + System.currentTimeMillis() - start;
} else {
return total;
}
}
/**
* Check if the watch is running.
*
* @return True if the watch is running.
*/
public boolean isRunning() {
return running;
}
/**
* Return a string representation.
*/
public String toString() {
StringBuffer buff = new StringBuffer();
if (running) {
// the watch has not been stopped
formatElapsedTime(buff, getTime());
// add the current lap time too if there is more than one lap
if (count >= 1) {
buff.append(", count=").append(count);
buff.append(", current=");
formatElapsedTime(buff, getLapTime());
}
} else {
// the watch has been stopped
formatElapsedTime(buff, getTime());
// add extra info if there is more than one lap
if (count > 1) {
buff.append(", count=").append(count);
buff.append(", average=");
formatElapsedTime(buff, getAverageLapTime());
}
}
return buff.toString();
}
private void formatElapsedTime(final StringBuffer buff, final long lapsed) {
long m = lapsed / 60000;
if (m != 0) {
buff.append(m).append("m:");
}
long s = (lapsed - 60000 * m) / 1000;
if (s != 0) {
buff.append(s).append("s:");
}
// ms is always there, even if it was 0 too
long ms = (lapsed - 60000 * m - 1000 * s);
buff.append(ms).append("ms");
}
/**
* Return a cloned copy of this object.
*
* @return A cloned copy of this object.
*/
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
// ///////////////////////////////////////////////////////////////////////
// Wrappers //
// ///////////////////////////////////////////////////////////////////////
/**
* Base wrapper class for other wrappers.
*/
private static class Wrapper extends StopWatch {
/** The serialVersionUID */
private static final long serialVersionUID = 6859401939735540773L;
protected StopWatch watch;
public Wrapper(final StopWatch watch) {
this.watch = watch;
}
public void start(final boolean reset) {
watch.start(reset);
}
public void start() {
watch.start();
}
public long stop() {
return watch.stop();
}
public void reset() {
watch.reset();
}
public long getLapTime() {
return watch.getLapTime();
}
public long getAverageLapTime() {
return watch.getAverageLapTime();
}
public int getLapCount() {
return watch.getLapCount();
}
public long getTime() {
return watch.getTime();
}
public boolean isRunning() {
return watch.isRunning();
}
public String toString() {
return watch.toString();
}
}
/**
* Return a synchronized stop watch.
*
* @param watch
* StopWatch to synchronize.
* @return Synchronized stop watch.
*/
public static StopWatch makeSynchronized(final StopWatch watch) {
return new Wrapper(watch) {
/** The serialVersionUID */
private static final long serialVersionUID = -6284244000894114817L;
public synchronized void start(final boolean reset) {
this.watch.start(reset);
}
public synchronized void start() {
this.watch.start();
}
public synchronized long stop() {
return this.watch.stop();
}
public synchronized void reset() {
this.watch.reset();
}
public synchronized long getLapTime() {
return this.watch.getLapTime();
}
public synchronized long getAverageLapTime() {
return this.watch.getAverageLapTime();
}
public synchronized int getLapCount() {
return this.watch.getLapCount();
}
public synchronized long getTime() {
return this.watch.getTime();
}
public synchronized boolean isRunning() {
return this.watch.isRunning();
}
public synchronized String toString() {
return this.watch.toString();
}
};
}
}
Some simple stop watch.
/*
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
/*
* Ein Klasse, die Zeitmessungen aufnimmt und diese in Relation zueineander
* setzt. Zum Start der Zeitmessung Methode start kurzer Beschreibung was
* gemessen wird als Parameter aufrufen. Das Ende der Zeitmessung wird durch
* stop angezeigt. Es kann nur eine Zeitmessung gleichzeitig stattfinden. Die
* Ausgabe ist nicht sortiert, gibt aber die relativen Unterschiede in der
* Dauer der einzelnen Messungen an. Die Messung mit der laengsten Dauer ist
* der Referenzwert (1.0).
*/
/**
* Some simple stop watch. It allows to measure multiple time periods
* and prints them. Usage: call start(comment) and stop() for
* each period of time.
*
* @author
*/
public class TimeMeasure {
protected final static double RESOLUTION = 100.0;
/**
* List of measurements.
*/
protected final ArrayList measures;
/**
* Message formatter
*/
protected final MessageFormat formatter;
/**
* the current time measurement.
*/
protected Measure current;
/**
* Simple TimeMesaure with default format.
*/
public TimeMeasure() {
this(new MessageFormat("{0}\t: {1}\t {2}x\n"));
}
/**
* A new TimeMeasure which reports in a specific format. The
* format is a standard MessageFormat with the following variables:
* <ul>
* <li><code>{0}</code> the measurement comment</li>
* <li><code>{1}</code> the time it took</li>
* <li><code>{2}</code> how many times this is faster than the
* slowest measurement</li>
* </ul>
*/
public TimeMeasure(MessageFormat formatter) {
this.measures = new ArrayList();
this.formatter = formatter;
}
/**
* Reset of all Measurements.
*/
public void reset() {
measures.clear();
}
/*
* Startet eine neue Messsung.
* @param comment Die Beschreibung der Messung.
*/
public void start(String comment) {
current = new Measure(comment);
}
/*
* Startet eine neue Messsung.
* @param comment Die Beschreibung der Messung.
public Object generate(String comment) {
current = new Measure();
actual.rument = comment;
measures.add(actual);
return actual;
}
*/
/*
* Addiert eine Messsung zu einer bestehenden.
* @param comment Die Beschreibung der Messung.
public void addToMeasure(Object measure) {
int index = measures.indexOf(measure);
if ( index<0 ) {
System.err.println("Measure does not exists " + measure);
actual = null;
return;
}
actual = (Measure)measures.get(index);
measures.remove(index);
actual.start = System.currentTimeMillis();
}
*/
/**
* stop current time measurement and store it.
*/
public void stop() {
if (current != null) {
current.stop();
measures.add(current);
current = null;
}
}
/**
* determines the time duration of the longest or shortest time interval.
*
* @param findShortest boolean "true", if we are looking for the shortest
* time interval; "false" if we are looking for the
* longest.
*/
private long findReferenceValue(boolean findShortest) {
long result = findShortest ? Long.MAX_VALUE : -1;
Iterator it = measures.iterator();
while (it.hasNext()) {
Measure m = (Measure) it.next();
result = (findShortest
? Math.min(result, m.getDuration())
: Math.max(result, m.getDuration()));
}
return result;
}
public String print() {
return print(false);
}
/**
* creates a formatted output (using the MessageFormat) of all
* results. The output is sorted in the in the sequence the time
* measurements took place.
* Writes the relative time to either the shortest or the longest
* time interval.
*
* @param shortestIsReference boolean true, if the shortest time interval
* is the reference value (1.0). False, if the
* longest is the reference.
*/
public String print(boolean shortestIsReference) {
StringBuilder result = new StringBuilder();
long reference = findReferenceValue(shortestIsReference);
Iterator it = measures.iterator();
while (it.hasNext()) {
Measure m = (Measure) it.next();
String factor = " -- ";
long duration = m.getDuration();
if (reference > 0) {
long tmp = (long) ((duration * RESOLUTION) / reference);
factor = String.valueOf(tmp / RESOLUTION);
}
Object[] args = {m.getComment(), (duration + "ms"), factor};
result.append(formatter.format(args));
}
return result.toString();
}
public String toString() {
return print();
}
/**
* A class to store one period of time.
*/
private final static class Measure {
/**
* start time.
*/
private final long start;
/**
* stop time.
*/
private long stop;
/**
* Die Gesamtdauer der Messung
*/
private long duration;
/**
* Description.
*/
private String comment;
public Measure(String comment) {
start = System.currentTimeMillis();
this.rument = comment;
}
public void stop() {
stop = System.currentTimeMillis();
duration = stop - start;
}
public long getDuration() { return duration; }
public String getComment() { return comment; }
}
}
Stop Watch from JGraphT
/*
* JGraphT : a free Java graph-theory library
*
*
* Project Info: http://jgrapht.sourceforge.net/
* Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
*
* (C) Copyright 2003-2007, by Barak Naveh and Contributors.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* -----------------
* StopWatch.java
* -----------------
* (C) Copyright 2005-2007, by Assaf Lehr and Contributors.
*
* Original Author: Assaf Lehr
* Contributor(s): -
*
* $Id: StopWatch.java 568 2007-09-30 00:12:18Z perfecthash $
*
* Changes
* -------
*/
/**
* @author Assaf
* @since May 30, 2005
*/
public class StopWatch
{
//~ Instance fields --------------------------------------------------------
long beforeTime;
//~ Methods ----------------------------------------------------------------
public void start()
{
this.beforeTime = System.currentTimeMillis();
}
public void stopAndReport()
{
long deltaTime = System.currentTimeMillis() - beforeTime;
if (deltaTime > 9999) {
double deltaTimeSec = deltaTime / 1000.0;
System.out.println(
"# Performence: " + deltaTimeSec + " full Seconds");
} else {
String timeDesc;
timeDesc =
(deltaTime <= 10) ? "<10ms [less than minumun measurement time]"
: String.valueOf(deltaTime);
System.out.println("# Performence: in MiliSeconds:" + timeDesc);
}
}
}
// End StopWatch.java
Stopwatch is a debugging tool for manually profiling code execution
package com.dbxml.util;
/*
* dbXML - Native XML Database
* Copyright (C) 1999-2004 The dbXML Group, L.L.C.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: Stopwatch.java,v 1.2 2004/02/12 00:17:58 bradford Exp $
*/
/**
* Stopwatch is a debugging tool for manually profiling code execution.
* This class will probably be removed for a production release.
*/
public final class Stopwatch {
private String label;
private long total = 0;
private long begin = 0;
private long end = 0;
public Stopwatch() {
}
public Stopwatch(boolean immediate) {
if ( immediate )
start();
}
public Stopwatch(String label) {
this.label = label;
}
public Stopwatch(String label, boolean immediate) {
this.label = label;
if ( immediate )
start();
}
public void start() {
begin = System.currentTimeMillis();
}
public long stop() {
end = System.currentTimeMillis();
total += (end - begin);
return total;
}
public void cancel() {
begin = 0;
end = 0;
}
public void reset() {
total = 0;
begin = 0;
end = 0;
}
public long elapsed() {
if ( end != 0 )
return end - begin;
else
return System.currentTimeMillis() - begin;
}
public long total() {
if ( end != 0 )
return total;
else
return (System.currentTimeMillis() - begin) + total;
}
public String toString() {
return toString(total());
}
public String toString(long t) {
StringBuffer sb = new StringBuffer();
if ( label != null )
sb.append(label + ": ");
long hour = t / 3600000;
if ( hour > 0 ) {
sb.append(hour + "h ");
t = t % 3600000;
}
long min = t / 60000;
if ( min > 0 ) {
sb.append(min + "m ");
t = t % 60000;
}
long sec = t / 1000;
if ( sec > 0 ) {
sb.append(sec + "s ");
t = t % 1000;
}
if ( t > 0 || total() == 0 )
sb.append(t + "ms");
return sb.toString();
}
}
StopWatch provides a convenient API for timings.
import java.util.Date;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* <p>
* <code>StopWatch</code> provides a convenient API for timings.
* </p>
*
* <p>
* To start the watch, call {@link #start()}. At this point you can:
* </p>
* <ul>
* <li>{@link #split()} the watch to get the time whilst the watch continues in the background. {@link #unsplit()} will
* remove the effect of the split. At this point, these three options are available again.</li>
* <li>{@link #suspend()} the watch to pause it. {@link #resume()} allows the watch to continue. Any time between the
* suspend and resume will not be counted in the total. At this point, these three options are available again.</li>
* <li>{@link #stop()} the watch to complete the timing session.</li>
* </ul>
*
* <p>
* It is intended that the output methods {@link #toString()} and {@link #getTime()} should only be called after stop,
* split or suspend, however a suitable result will be returned at other points.
* </p>
*
* <p>
* NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start,
* resume before suspend or unsplit before split.
* </p>
*
* <p>
* 1. split(), suspend(), or stop() cannot be invoked twice<br />
* 2. unsplit() may only be called if the watch has been split()<br />
* 3. resume() may only be called if the watch has been suspend()<br />
* 4. start() cannot be called twice without calling reset()
* </p>
*
* @author Stephen Colebourne
* @since 2.0
* @version $Id: StopWatch.java 583608 2007-10-10 20:38:41Z ggregory $
*/
public class StopWatch {
// running states
private static final int STATE_UNSTARTED = 0;
private static final int STATE_RUNNING = 1;
private static final int STATE_STOPPED = 2;
private static final int STATE_SUSPENDED = 3;
// split state
private static final int STATE_UNSPLIT = 10;
private static final int STATE_SPLIT = 11;
/**
* The current running state of the StopWatch.
*/
private int runningState = STATE_UNSTARTED;
/**
* Whether the stopwatch has a split time recorded.
*/
private int splitState = STATE_UNSPLIT;
/**
* The start time.
*/
private long startTime = -1;
/**
* The stop time.
*/
private long stopTime = -1;
/**
* <p>
* Constructor.
* </p>
*/
public StopWatch() {
super();
}
/**
* <p>
* Start the stopwatch.
* </p>
*
* <p>
* This method starts a new timing session, clearing any previous values.
* </p>
*
* @throws IllegalStateException
* if the StopWatch is already running.
*/
public void start() {
if (this.runningState == STATE_STOPPED) {
throw new IllegalStateException("Stopwatch must be reset before being restarted. ");
}
if (this.runningState != STATE_UNSTARTED) {
throw new IllegalStateException("Stopwatch already started. ");
}
this.stopTime = -1;
this.startTime = System.currentTimeMillis();
this.runningState = STATE_RUNNING;
}
/**
* <p>
* Stop the stopwatch.
* </p>
*
* <p>
* This method ends a new timing session, allowing the time to be retrieved.
* </p>
*
* @throws IllegalStateException
* if the StopWatch is not running.
*/
public void stop() {
if (this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) {
throw new IllegalStateException("Stopwatch is not running. ");
}
if (this.runningState == STATE_RUNNING) {
this.stopTime = System.currentTimeMillis();
}
this.runningState = STATE_STOPPED;
}
/**
* <p>
* Resets the stopwatch. Stops it if need be.
* </p>
*
* <p>
* This method clears the internal values to allow the object to be reused.
* </p>
*/
public void reset() {
this.runningState = STATE_UNSTARTED;
this.splitState = STATE_UNSPLIT;
this.startTime = -1;
this.stopTime = -1;
}
/**
* <p>
* Split the time.
* </p>
*
* <p>
* This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected,
* enabling {@link #unsplit()} to continue the timing from the original start point.
* </p>
*
* @throws IllegalStateException
* if the StopWatch is not running.
*/
public void split() {
if (this.runningState != STATE_RUNNING) {
throw new IllegalStateException("Stopwatch is not running. ");
}
this.stopTime = System.currentTimeMillis();
this.splitState = STATE_SPLIT;
}
/**
* <p>
* Remove a split.
* </p>
*
* <p>
* This method clears the stop time. The start time is unaffected, enabling timing from the original start point to
* continue.
* </p>
*
* @throws IllegalStateException
* if the StopWatch has not been split.
*/
public void unsplit() {
if (this.splitState != STATE_SPLIT) {
throw new IllegalStateException("Stopwatch has not been split. ");
}
this.stopTime = -1;
this.splitState = STATE_UNSPLIT;
}
/**
* <p>
* Suspend the stopwatch for later resumption.
* </p>
*
* <p>
* This method suspends the watch until it is resumed. The watch will not include time between the suspend and
* resume calls in the total time.
* </p>
*
* @throws IllegalStateException
* if the StopWatch is not currently running.
*/
public void suspend() {
if (this.runningState != STATE_RUNNING) {
throw new IllegalStateException("Stopwatch must be running to suspend. ");
}
this.stopTime = System.currentTimeMillis();
this.runningState = STATE_SUSPENDED;
}
/**
* <p>
* Resume the stopwatch after a suspend.
* </p>
*
* <p>
* This method resumes the watch after it was suspended. The watch will not include time between the suspend and
* resume calls in the total time.
* </p>
*
* @throws IllegalStateException
* if the StopWatch has not been suspended.
*/
public void resume() {
if (this.runningState != STATE_SUSPENDED) {
throw new IllegalStateException("Stopwatch must be suspended to resume. ");
}
this.startTime += (System.currentTimeMillis() - this.stopTime);
this.stopTime = -1;
this.runningState = STATE_RUNNING;
}
/**
* <p>
* Get the time on the stopwatch.
* </p>
*
* <p>
* This is either the time between the start and the moment this method is called, or the amount of time between
* start and stop.
* </p>
*
* @return the time in milliseconds
*/
public long getTime() {
if (this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) {
return this.stopTime - this.startTime;
} else if (this.runningState == STATE_UNSTARTED) {
return 0;
} else if (this.runningState == STATE_RUNNING) {
return System.currentTimeMillis() - this.startTime;
}
throw new RuntimeException("Illegal running state has occured. ");
}
/**
* <p>
* Get the split time on the stopwatch.
* </p>
*
* <p>
* This is the time between start and latest split.
* </p>
*
* @return the split time in milliseconds
*
* @throws IllegalStateException
* if the StopWatch has not yet been split.
* @since 2.1
*/
public long getSplitTime() {
if (this.splitState != STATE_SPLIT) {
throw new IllegalStateException("Stopwatch must be split to get the split time. ");
}
return this.stopTime - this.startTime;
}
/**
* Returns the time this stopwatch was started.
*
* @return the time this stopwatch was started
* @throws IllegalStateException
* if this StopWatch has not been started
* @since 2.4
*/
public long getStartTime() {
if (this.runningState == STATE_UNSTARTED) {
throw new IllegalStateException("Stopwatch has not been started");
}
return this.startTime;
}
/**
* <p>
* Gets a summary of the time that the stopwatch recorded as a string.
* </p>
*
* <p>
* The format used is ISO8601-like, <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
* </p>
*
* @return the time as a String
*/
public String toString() {
return new Date(getTime()).toString();
}
/**
* <p>
* Gets a summary of the split time that the stopwatch recorded as a string.
* </p>
*
* <p>
* The format used is ISO8601-like, <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.
* </p>
*
* @return the split time as a String
* @since 2.1
*/
public String toSplitString() {
return new Date(getSplitTime()).toString();
}
}
StopWatch reports elapsed time between start and stop
/*
* Copyright 2003-2004 Michael Franken, Zilverline.
*
* The contents of this file, or the files included with this file, are subject to
* the current version of ZILVERLINE Collaborative Source License for the
* Zilverline Search Engine (the "License"); You may not use this file except in
* compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.zilverline.org.
*
* See the License for the rights, obligations and
* limitations governing use of the contents of the file.
*
* The Original and Upgraded Code is the Zilverline Search Engine. The developer of
* the Original and Upgraded Code is Michael Franken. Michael Franken owns the
* copyrights in the portions it created. All Rights Reserved.
*
*/
/**
* StopWatch reports elapsed time between start and stop.
*
* @author Michael Franken
* @version $Revision: 1.12 $
*/
public class StopWatch {
/** Time the Stopwatch started. */
private long start = 0;
/** Time the Stopwatch stopped. */
private long stop = 0;
/**
* Start ticking, resets the watch.
*/
public final void start() {
start = System.currentTimeMillis();
}
/**
* Stop ticking.
*/
public final void stop() {
stop = System.currentTimeMillis();
}
/**
* Calculates time elapsed. If stop has not been called yet, the current time is taken to calculate elapsed time.
*
* @return the time elapsed between start and stop as String. The string contains two of days, hours, minutes, seconds and
* milliseconds.
*
* @throws StopWatchException If StopWatch was never started.
*/
public final String elapsedTime() {
long difference;
if (stop == 0) {
long now = System.currentTimeMillis();
difference = (now - start); // in millis
} else {
difference = (stop - start); // in millis
}
long mils = difference % 1000;
difference = (difference - mils) / 1000; // in seconds
long secs = difference % 60;
difference = (difference - secs) / 60; // in minutes
long minutes = difference % 60;
difference = (difference - minutes) / 60; // in hours
long hours = difference % 24;
difference = (difference - hours) / 24; // in days
long days = difference;
String message = "";
if (days > 0) {
message = days + " days and " + hours + " hours";
} else if (hours > 0) {
message = hours + " hours and " + minutes + " minutes";
} else if (minutes > 0) {
message = minutes + " minutes and " + secs + " seconds";
} else if (secs > 0) {
message = secs + " seconds and " + mils + " milliseconds";
} else {
message = mils + " milliseconds";
}
return message;
}
}
Stop watch with nano second
public class StopWatch {
public final long t0 = System.currentTimeMillis();
public final long n0 = System.nanoTime();
static public StopWatch start() {
return new StopWatch();
}
public long millisEllapsed() {
return System.currentTimeMillis() - t0;
}
public double secondsEllapsed() {
return millisEllapsed() / 1000.0;
}
public long nanoEllapsed() {
return System.nanoTime() - n0;
}
}
Stop Watch with PrintWriter
import java.io.PrintWriter;
/**
* Prints time durations; used for development purposes only.
* <p>
* No threads or system resources are harmed in the making of a stop watch. A
* stop watch simply remembers the start time (and elapsed time when paused) and
* prints the total "running" time when either {@link #mark} or
* {@link #stop} is called.
* <p>
* {@link #stop} doesn"t really stop anything. You can call stop as many times
* as you like and it will print the time elapsed since start. It would be easy
* to change this behavior, but I haven"t needed to.
*
* @author Jim Menard,
*/
public class StopWatch {
protected String name;
protected long t0;
protected long elapsedTime;
protected PrintWriter out;
public StopWatch() {
this(null, null);
}
public StopWatch(String name) {
this(name, null);
}
public StopWatch(PrintWriter out) {
this(null, out);
}
public StopWatch(String name, PrintWriter out) {
this.name = name;
if (out == null)
this.out = new PrintWriter(System.err);
else
this.out = out;
elapsedTime = -1L; // So we can tell if we are ever started
}
/**
* Remembers the current time and prints a message.
*/
public void start() {
start(true);
}
/**
* Remembers the current time and prints a message if requested.
*
* @param printStarting
* if <code>true</code> and this stop watch has a name, print a
* message
*/
public void start(boolean printStarting) {
if (t0 != 0)
System.err.println("(warning: StopWatch already started; resetting)");
if (printStarting && name != null)
System.err.println("starting " + name);
elapsedTime = 0;
t0 = System.currentTimeMillis();
}
/**
* Pauses the stop watch.
*/
public void pause() {
long now = System.currentTimeMillis();
elapsedTime += now - t0;
t0 = 0;
}
/**
* Resumes the stop watch.
*/
public void resume() {
t0 = System.currentTimeMillis();
}
/**
* Prints the current elapsed time without stopping.
*/
public void mark() {
stop(null, true);
}
/**
* Prints the current elapsed time without stopping, along with the stop watch
* name if <var>printMark</var> is <code>true</code>.
*
* @param printMark
* if <code>true</code>, the stop watch name will be printed
*/
public void mark(boolean printMark) {
stop(null, printMark);
}
/**
* Prints the current elapsed time without stopping, along with the stop watch
* name and <var>msg</var>.
*
* @param msg
* a message to print
*/
public void mark(String msg) {
stop(msg, true);
}
/**
* Prints the current elapsed time without stopping, along with, along with
* the stop watch name if <var>printMark</var> is <code>true</code> and the
* <var>msg</var> if it"s not <code>null</code>.
*
* @param msg
* a message to print
* @param printMark
* if <code>true</code>, the stop watch name will be printed
*/
public void mark(String msg, boolean printMark) {
stop(msg, printMark);
}
/**
* Stops the stop watch and prints the name of this stop watch and the current
* elapsed time.
*/
public void stop() {
stop(null, true);
}
/**
* Stops the stop watch and prints the name of this stop watch, <var>msg</var>
* if non-<code>null</code>, and the current elapsed time.
*
* @param msg
* a message to print; may be <code>null</code>
*/
public void stop(String msg) {
stop(msg, true);
}
/**
* Prints the current elapsed time, along with the stop watch name if
* <var>printMark</var> is <code>true</code> and the <var>msg</var> if
* it"s not <code>null</code>.
*
* @param msg
* a message to print; may be <code>null</code>
* @param printName
* if <code>true</code>, the stop watch name will be printed
*/
public void stop(String msg, boolean printName) {
long now = System.currentTimeMillis();
if (elapsedTime == -1) {
System.err.println("(StopWatch" + (name != null ? (" \"" + name + """) : "")
+ " was stopped without ever being started)");
return;
}
long total = elapsedTime;
if (t0 != 0)
total += now - t0;
String separator = null;
if (printName && name != null) {
System.err.print(name);
separator = ": ";
}
if (msg != null) {
if (separator != null)
System.err.print(" ");
System.err.print("(" + msg + ")");
separator = ": ";
}
if (separator != null)
System.err.print(separator);
System.err.println("" + (total / 1000.0) + " seconds");
}
}
Your own timer
/*
* Copyright (c) 1998-2002 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS"" AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
import java.util.Vector;
public class Timer {
int interval;
boolean periodic;
boolean isExpired = false;
static TimerManager manager = new TimerManager ();
long deadline;
Timer next, prev;
public Timer () {
}
public void set (int msecDelay, boolean periodic) {
interval = msecDelay;
this.periodic = periodic;
isExpired = false;
if (!manager.isAlive ()) {
System.err.println ("TimerManager: restarting");
manager = new TimerManager ();
}
manager.register (this, System.currentTimeMillis () + msecDelay);
}
public int getInterval () {
return interval;
}
public boolean getPeriodic () {
return periodic;
}
public void cancel () {
manager.delete (this);
}
protected void alarm () {
}
public boolean expired () {
return isExpired;
}
/*
public static void main (String[] args) {
for (int i=0; i<args.length; ++i) {
boolean periodic = (args[i].charAt (0) == "p");
if (periodic) args[i] = args[i].substring (1);
new TestTimer (args[i], Integer.parseInt (args[i]), periodic);
}
while (true) Thread.yield ();
}
*/
}
class TimerManager extends Thread {
Timer first, last;
/*
static ThreadGroup rootThreadGroup;
static {
rootThreadGroup = Thread.currentThread().getThreadGroup();
while (rootThreadGroup.getParent() != null)
rootThreadGroup = rootThreadGroup.getParent();
}
*/
public TimerManager () {
super (/* rootThreadGroup, */ "Timer Manager");
setDaemon (true);
start ();
}
public synchronized void register (Timer t, long deadline) {
t.deadline = deadline;
delete (t); // just in case it"s already registered
//System.err.println ("TimerManager: set " + t + " to go off at " + deadline);
insertion:
{
for (Timer u = first; u != null; u = u.next) {
if (t.deadline < u.deadline) {
if (u.prev != null)
u.prev.next = t;
else
first = t;
t.prev = u.prev;
t.next = u;
u.prev = t;
break insertion;
}
}
if (last != null) {
last.next = t;
t.prev = last;
t.next = null;
last = t;
} else {
first = last = t;
}
}
//System.err.println ("TimerManager: waking up background thread");
notifyAll ();
}
public synchronized void delete (Timer t) {
if (t.next != null)
t.next.prev = t.prev;
if (t.prev != null)
t.prev.next = t.next;
if (t == last)
last = t.prev;
if (t == first)
first = t.next;
t.next = null;
t.prev = null;
}
static final int FOREVER = 60000; // wake up at least every 60 seconds
public synchronized void run () {
while (true) {
try {
//System.err.println ("TimerManager: awake");
if (first == null) {
//System.err.println ("TimerManager: waiting forever");
wait (FOREVER);
//System.err.println ("TimerManager: woke up");
}
else {
Timer t = first;
long now = System.currentTimeMillis ();
if (t.deadline <= now) {
// System.err.println ("TimerManager: timer " + t + " just went off at " + now);
try {
t.isExpired = true;
t.alarm ();
} catch (Throwable e) {
if (e instanceof ThreadDeath)
throw (ThreadDeath)e;
else
e.printStackTrace ();
}
if (t.periodic) {
register (t, now + t.interval);
}
else {
delete (t);
}
}
else {
//System.err.println ("TimerManager: waiting for " + (t.deadline - now) + " msec");
wait (t.deadline - now);
//System.err.println ("TimerManager: woke up");
}
}
} catch (InterruptedException e) {}
}
}
}
/*
class TestTimer extends Timer {
String message;
public TestTimer (String message, int millisec, boolean periodic) {
this.message = message;
set (millisec, periodic);
}
public void alarm () {
System.out.println (message);
}
}
*/