Dica... Me parece que vc quer fazer uma tarefa que para e continua, certo?
O java tem classes de Timer prontas para isso, java.util.timer e java.swing.timer.
Eu também já desenvolvi uma, com a opção de pausar o timer e continuar de onde parou. A classe está aqui embaixo, para caso você queira estudar:
/**
* The <code>Chronometer</code> class is like a timer, but it can stop
* counting time when desired and continue counting later. <br>
* Chronometer starts counting time by calling the <code>start()</code>
* method. When the time limit is reached, the chronometer fires a
* <code>TimeEvent</code> for all it's listeners. If the user calls the
* <code>stop()</code> method, the chronomether will freeze its counting. The
* count sequence will <i>proceed</i> when the users call again the start
* method. To restart counting, user may call <code>reset()</code> method at
* anytime (even when the chronometer is running).
* <p>
* Chronometers can be ciclical or non-ciclical. A non-ciclical chronometer will
* stop automatically when the timeLimit is exceeded. Then, it will reset itself
* automatically. A ciclical chronometer will reset and counting again. So, it
* will generate chronometer events until the user stops counting manually. The
* precision parameter define the time, in miliseconds, that chronometer will
* actualize itself. For example, in a chronomether with precision 50, the
* chronometer will process each 50 miliseconds, so the current time will be 0,
* 50, 100, 150 and so on. Smaller precision is usefull only when
* <code>start()</code> and <code>stop()</code> will be used wihout calling
* </code>reset()</code>. Smaller precision also demand more CPU resources.
* The minimum precision of a chronometer is 5ms. The default chronometer
* parameters are:
* <ul>
* <li>30000 time limit;
* <li>1000 precision;
* <li>0 current time;
* <li>not ciclical;
* </ul>
* <p>
*
* @see ChronometerListener
* @see TimeEvent
* @author Mendonça, Vinícius Godoy de
*/
public class Chronometer {
private volatile int currentTime = 0;
private volatile int timeLimit = 30000;
private volatile boolean isCiclic = false;
private volatile int precision = 1000;
private Engine engine = null;
private String name = Chronometer.class.getName();
private EventListenerList listenersList = new EventListenerList();
public static final int MINIMUM_TIME_LIMIT = 10;
public static final int MINIMUM_PRECISION = 5;
/**
* Notify all listeners that have registered interest for notification on
* this undecoded message events. The event instance is lazily created using
* the parameters passed into the fire method.
*/
protected void fireTimeEvent() {
TimeEvent evt = null;
// Guaranteed to return a non-null array
Object[] listeners = listenersList.getListenerList();
// Process the listeners first to last, notifying
// those that are interested in this event
for (int i = 0; i <= listeners.length - 2; i += 2)
if (listeners[i] == ChronometerListener.class) {
// Lazily create the event:
if (evt == null)
evt = new TimeEvent(this);
((ChronometerListener) listeners[i + 1]).timeExceeded(evt);
}
}
/**
* Creates a new <code>Chronometer</code>. <br>
* The default chronometer parameters are:
* <ul>
* <li>30000 time limit;
* <li>1000 precision;
* <li>0 current time;
* <li>not ciclical;
* </ul>
*/
public Chronometer() {
this(Chronometer.class.getSimpleName());
}
/**
* Creates a new <code>Chronometer</code> with the given name. <br>
* The default chronometer parameters are:
* <ul>
* <li>30000 time limit;
* <li>1000 precision;
* <li>0 current time;
* <li>not ciclical;
* </ul>
*/
public Chronometer(String name) {
super();
currentTime = 0;
setName(name);
}
/**
* Creates a new <code>Chronometer</code> with the given listener. <br>
* The default chronometer parameters are:
* <ul>
* <li>30000 time limit;
* <li>1000 precision;
* <li>0 current time;
* <li>not ciclical;
* </ul>
*/
public Chronometer(ChronometerListener listener) {
this();
addListener(listener);
}
/**
* Creates a new <code>Chronometer</code> with the given name and
* listener. <br>
* The default chronometer parameters are:
* <ul>
* <li>30000 time limit;
* <li>1000 precision;
* <li>0 current time;
* <li>not ciclical;
* </ul>
*/
public Chronometer(String name, ChronometerListener listener) {
this(name);
addListener(listener);
}
/**
* Sets the chronometer as ciclical or non-ciclical. A non-ciclical
* chronometer will stop automatically when the time limit is exceeded.
* Then, it will reset itself automatically. A ciclical chronometer will
* reset and counting again. So, it will generate chronometer events until a
* call to stop()
*
* @param ciclic A boolean flag indicating if the chronometer is ciclical.
* @see Chronometer#stop()
*/
public void setCiclic(boolean ciclic) {
this.isCiclic = ciclic;
}
/**
* Indicate if the chronometer is ciclical or non-ciclical. A non-ciclical
* chronometer will stop automatically when the time limit is exceeded.
* Then, it will reset itself automatically. A ciclical chronometer will
* reset and counting again. So, it will generate chronometer events until a
* call to stop()
*
* @see Chronometer#stop()
* @see Chronometer#setCiclic(boolean)
*/
public boolean isCiclic() {
return isCiclic;
}
/**
* Defines a new precision for this chronometer. The precision parameter
* define the time, in miliseconds, that chronometer will actualize itself.
* For example, in a chronomether with precision 50, the chronometer will
* process each 50 miliseconds, so the current time will be 0, 50, 100, 150
* and so on. Smaller precision is usefull only when <code>start()</code>
* and <code>stop()</code> will be used wihout calling </code>reset()</code>.
* Smaller precision also demand more CPU resources. The minimum precision
* of a chronometer is 5ms.
*
* @param miliseconds The new precision in miliseconds.
*/
public void setPrecision(int miliseconds) throws IllegalArgumentException {
if (miliseconds < MINIMUM_PRECISION)
throw new IllegalArgumentException(
"Precision cannot be smaller than" + MINIMUM_PRECISION
+ ".");
precision = miliseconds;
}
/**
* Retrieves the current precision. The precision parameter define the time,
* in miliseconds, that chronometer will actualize itself. For example, in a
* chronomether with precision 50, the chronometer will process each 50
* miliseconds, so the current time will be 0, 50, 100, 150 and so on.
* Smaller precision is usefull only when <code>start()</code> and
* <code>stop()</code> will be used wihout calling </code>reset()</code>.
* Smaller precision also demand more CPU resources. The minimum precision
* of a chronometer is 5ms.
*/
public int getPrecision() {
return precision;
}
/**
* Time that, when reached, make the chronometer fire its
* <code>TimeEvent</code>
*
* @param miliseconds Time, in miliseconds.
*/
public void setTimeLimit(int miliseconds) {
if (miliseconds < MINIMUM_TIME_LIMIT)
throw new IllegalArgumentException(
"Time Limit cannot be smaller than " + MINIMUM_TIME_LIMIT
+ ".");
timeLimit = miliseconds;
}
/**
* Current elapsed time for this <code>Chronometer</code>. Readings of
* this parameter is directly afected by the chonometer precision.
*
* @see Chronometer#getPrecision()
*/
public int getCurrentTime() {
return currentTime;
}
/**
* Start counting the time.
*
* @see Chronometer#stop()
* @see Chronometer#reset()
*/
public synchronized void start() {
if (engine != null)
return;
engine = new Engine(getName());
engine.start();
}
/**
* Stop counting time. This action will not reset the
* <code>Chronometer</code>.
*
* @see Chronometer#start()
* @see Chronometer#reset()
*/
public synchronized void stop() {
if (engine == null)
return;
engine.interrupt();
engine = null;
}
/**
* Makes the chronometer restart it's time counting.
*
* @see Chronometer#start()
* @see Chronometer#stop()
*/
public void reset() {
currentTime = 0;
}
/**
* Adds a listener for this <code>Chronometer</code>.
*
* @see ChronometerListener
*/
public void addListener(ChronometerListener listener) {
listenersList.add(ChronometerListener.class, listener);
}
/**
* Removes the given listener from this <code>Chronometer</code>.
*
* @param listener The listener to remove.
*/
public void removeListener(ChronometerListener listener) {
listenersList.remove(ChronometerListener.class, listener);
}
/**
* Returns this chronometer name.
*
* @return The chronometer name.
*/
public String getName() {
return name;
}
/**
* Sets this chronometer name. This same name will be used in the
* chronometer thread every time the chronometer is running.
*
* @param name The chronometer name.
*/
public void setName(String name) {
if (name == null || name.trim().equals(""))
throw new IllegalArgumentException("Name can't be null or blank!");
this.name = name;
}
private class Engine extends Thread
{
public Engine(String name) {
setDaemon(true);
setName(name);
}
@Override
public void run() {
try {
while (!isInterrupted()) {
currentTime += precision;
synchronized (this) {
wait(precision);
}
if (currentTime >= timeLimit) {
currentTime = 0;
fireTimeEvent();
if (!isCiclic())
Chronometer.this.stop();
}
}
}
catch (InterruptedException e) {
}
}
}
}
Esse é o listener dessa classe (quase me esqueço).
/**
* Listens for chronometer events.
*/
public interface ChronometerListener extends EventListener {
/**
@throws Exception
*/
public void timeExceeded(TimeEvent timeEvent);
}
O nome ficou infeliz pq originalmente era para ser uma classe de medição, mas acabou virando só um Timer espertinho mesmo. :)