当前位置: 技术问答>java相关
java中的定时如何实现
来源: 互联网 发布时间:2015-02-15
本文导语: java中是否有用来定时的类?若有,他是什么? 另外tomcat是什么?我在jb5中如何配置? | import sun.misc.*; // Sun's Timer undocumented class import java.util.*; // Calendar class public class Ti...
java中是否有用来定时的类?若有,他是什么?
另外tomcat是什么?我在jb5中如何配置?
另外tomcat是什么?我在jb5中如何配置?
|
import sun.misc.*; // Sun's Timer undocumented class
import java.util.*; // Calendar class
public class TimerTest implements Timeable {
public static void main(String[] args) {
TimerTest me = new TimerTest();
Timer ticker = new Timer(me, 1000);
ticker.cont();
System.out.println("Timer started");
}
public void tick(Timer t) {
System.out.println
("07Being ticked " + Calendar.getInstance().getTime());
}
}
The Timer class is now part of the Swing package. The Timer object will send an ActionEvent to the registered ActionListener. import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
public class TimerDemo implements ActionListener {
Timer t = new Timer(1000,this);
TimerDemo() {
t.start();
}
public static void main(String args[]) {
TimerDemo td = new TimerDemo();
// create a dummy frame to keep the JVM running
// (for demonstation purpose)
java.awt.Frame dummy = new java.awt.Frame();
dummy.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == t) {
System.out.println
("07Being ticked " + Calendar.getInstance().getTime());
}
}
}
import java.util.*; // Calendar class
public class TimerTest implements Timeable {
public static void main(String[] args) {
TimerTest me = new TimerTest();
Timer ticker = new Timer(me, 1000);
ticker.cont();
System.out.println("Timer started");
}
public void tick(Timer t) {
System.out.println
("07Being ticked " + Calendar.getInstance().getTime());
}
}
The Timer class is now part of the Swing package. The Timer object will send an ActionEvent to the registered ActionListener. import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
public class TimerDemo implements ActionListener {
Timer t = new Timer(1000,this);
TimerDemo() {
t.start();
}
public static void main(String args[]) {
TimerDemo td = new TimerDemo();
// create a dummy frame to keep the JVM running
// (for demonstation purpose)
java.awt.Frame dummy = new java.awt.Frame();
dummy.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == t) {
System.out.println
("07Being ticked " + Calendar.getInstance().getTime());
}
}
}
|
由阿,就是java.util.Timer不是楼上说的swing里面的,那个没有util里面的好用
你可以看看JDK1.3以后的稳当
你可以看看JDK1.3以后的稳当