📄 timer.java
字号:
package Jt.examples.patterns;
import Jt.*;
import java.util.*;
/**
* Timer implementation based on the Command pattern. The timer runs in a separate
* Thread.
*/
public class Timer extends JtCommand {
private static final long serialVersionUID = 1L;
public static final String JtCLASS_NAME = Timer.class.getName();
public static final String DISPLAY_TIME = "DISPLAY_TIME";
private long tstart = 0L; // t0
private long tend; // t1
private double time; // Elapsed time in seconds (delta)
public Timer() {
this.setSynchronous (false); // asynchronous processing (separate thread)
}
// Attributes
public double getTime () {
return (time);
}
public void setTime (double time) {
this.time = time;
}
// Process object messages
public Object processMessage (Object message) {
String msgid = null;
JtMessage msg = (JtMessage) message;
//Object content;
if (msg == null)
return null;
msgid = (String) msg.getMsgId ();
if (msgid == null)
return null;
// Start timer
if (msgid.equals (JtObject.JtSTART)) {
tstart = (new Date()).getTime ();
// Log the message
logMessage (msg);
// Let the superclass process JtSTART. A new thread is started
// to process Jt messages.
super.processMessage (message);
// Start displaying the time
enqueueMessage (new JtMessage (Timer.DISPLAY_TIME));
return (null);
}
// Display the time
if (msgid.equals (Timer.DISPLAY_TIME)) {
if (tstart == 0L)
tstart = (new Date()).getTime ();
tend = (new Date ()).getTime ();
time = (tend - tstart)/1000.0;
System.out.println ("Timer:" + time);
// Add another DISPLAY_TIME request to the queue of messages
//sendMessage (this, new JtMessage ("DISPLAY_TIME"));
enqueueMessage (new JtMessage (Timer.DISPLAY_TIME));
return (null);
}
// Stop the timer
/*
if (msgid.equals ("STOP_TIMER")) {
tend = (new Date ()).getTime ();
time = (tend - tstart)/1000.0;
//sendMessage (this, new JtMessage ("JtSTOP"));
super.processMessage(new JtMessage ("JtSTOP"));
// Log the message
logMessage (msg);
return (null);
}
*/
// Let the superclass handle JTSTOP, JtREMOVE, etc
return (super.processMessage (message));
//handleError ("Timer.processMessage: invalid message id:" + msgid);
//return (null);
}
static private char waitForInputKey () {
char c = ' ';
try {
c = (char) System.in.read ();
while (System.in.available () > 0)
System.in.read ();
} catch (Exception e) {
e.printStackTrace ();
}
return (c);
}
// Test program
public static void main(String[] args) {
JtObject main = new JtFactory ();
// Create the timer
main.createObject ("Jt.examples.patterns.Timer", "timer");
System.out.println ("Press any key to start/stop the timer ....");
waitForInputKey ();
// Start the timer using a separate/independent thread.
// Messages are processed Asynchronously
// via a message queue.
main.sendMessage ("timer", new JtMessage (JtObject.JtSTART));
waitForInputKey ();
// Stop the timer
main.sendMessage ("timer", new JtMessage (JtObject.JtSTOP));
System.out.println (main.getValue ("timer", "time") + " second(s) elapsed");
// Remove object
main.removeObject ("timer");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -