📄 boundedtimerx.java
字号:
package edu.odu.cs.zeil.AlgAE.Client;import java.util.Observable;import edu.odu.cs.zeil.AlgAE.Application;import edu.odu.cs.zeil.AlgAE.Debug;import edu.odu.cs.zeil.AlgAE.Termination;import edu.odu.cs.zeil.AlgAE.Client.IncrementControl;/** * A BoundedTimer is a clock that ticks at regular intervals for * a fixed, predetermined number of ticks. * * @author Steven J. Zeil */public class BoundedTimer extends Observable{ /** * Timer control information */ private IncrementControl control; /** * Is this timer being held by another thread? */ private boolean locked; /** * Time interval (in ms.) between ticks */ private long interval; /** * The thread used to measure off the time between ticks. */ private TimerThread timer; /** * Is the clock ticking? */ private boolean ticking; /** * Create a timer. * * @param increment structure describing the desired timing sequence */ public BoundedTimer (Application application) { super(); control = new IncrementControl(); interval = (long)(1000 * control.getIncrementTime()); ticking = false; Debug.show (Debug.timer, "application is " + application); Debug.show (Debug.timer, "Creating thread"); timer = new TimerThread(application); timer.start(); application.timerThread = timer; } public IncrementControl getControl() { return control; } /** * Prevent another thread from seizing or starting the timer. * This blocks if the timer is running or another thread has already * seized the timer. */ public synchronized void seize() { while (locked) { try { wait(); } catch (InterruptedException e) {} } locked = true; } /** * Allow another thread to seize or start the timer. */ public synchronized void release() { if (locked) { locked = false; notifyAll(); } } /** * Start the timer running. May be done multiple times. * This will block the calling thread if the timer has been seized. */ public synchronized void start() { control.setIncrementStep (0); interval = (long)(1000 * control.getIncrementTime()); ticking = true; } /** * Stop the timer (even if it has not yet run down). */ public void stop() { synchronized (this) { if (ticking) { Debug.show (Debug.timer, "Timer: stop"); int total = control.getIncrementTotal(); control.setIncrementStep (total); if (total > 0) { tick(); ticking = false; release(); } else ticking = false; } } } /** * Called by the thread at each time click. Notifies all registered * observers. */ private synchronized void tick() { if (ticking) { int step = control.getIncrementStep(); if (step == 0 && step < control.getIncrementTotal()) { Debug.show (Debug.timer, "Tick: seize"); seize(); } Debug.show (Debug.timer, "Tick", step); setChanged(); notifyObservers(new Integer(step)); if (control.completed()) { ticking = false; Debug.show (Debug.timer, "Tick: expired"); // stop(); if (step > 0) release(); } else control.advance(); } } /** * A thread that sends signals at regular intervals * * * @author Steven J. Zeil */ class TimerThread extends Thread { /** * Construct a timer thread. */ TimerThread (Application application) { // super(application.algaeThreads, "BoundedTimer"); super("BoundedTimer"); Debug.show (Debug.timer, "TimerThread constructed"); } /** * Start the clock ticking. */ public void run() { Debug.show (Debug.timer, "Starting thread\n"); tick(); while (true) { try { sleep(interval); } catch (InterruptedException e) {} tick(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -