请问谁有定时任务的源程序(比如每过1秒就打印“hello”),在线等!!!急急!!!
请问谁有定时任务的源程序(比如每过1秒就打印“hello”),在线等!!!急急!!! 问题点数:100、回复次数:6Top
1 楼qjhaaaaa()回复于 2004-09-02 14:02:32 得分 20
public class FileMonitor
extends Thread {
public FileMonitor() {
}
public static Log4J log4j = new Log4J();
Object cl;
String action;
int timeout;
public FileMonitor(Object cl, String action, int timeout) {
this.cl = cl;
this.action = action;
this.timeout = timeout;
}
public void run() {
while (true) {
Object o = new Object();
synchronized (o) {
try {
o.wait(timeout);
Method method = cl.getClass().getMethod(action, null);
method.invoke(cl, null);
}
catch (Exception ex) {
log4j.error.error("run() : " + ex.getMessage());
ex.getMessage();
}
}
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
Main main = new Main();
new FileMonitor(main, "FileMonitor", 10000).start();
}Top
2 楼yangjuanli(珂儿)回复于 2004-09-07 08:23:14 得分 20
public class AddZcm extends Thread
{
public AddZcm(){
}
public static void main(String[] args)
{ while(true)
{
try{
System.out.println("hello world!");
Thread.sleep(1000);
}
}
}
}Top
3 楼tjl713(tjl)回复于 2004-09-07 08:42:53 得分 20
yangjuanli(珂儿) :
你的程序可能会有问题!
如果用线程的休眠方法,那么程序表明至少等待1秒才能运行,但并不表明1秒后一定是这个线程被唤起继续运行,如果操作系统内有其它的线程,也有可能是它们被唤起,然后才是这个程序执行。
个人意见,大家讨论!Top
4 楼Acylas(Acylas)回复于 2004-09-07 08:54:35 得分 20
javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent event) {
System.out.println("hello");
}
});
或者用
java.util.Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
System.out.println("hello");
}
}, 0, 1000);
Top
5 楼sandyen(杉叶)回复于 2004-09-07 09:08:57 得分 20
public class Task{
public void run() {
System.out.println("Hello");
}}
public class Main{
java.util.Timer timer = new java.util.Timer();
public static void main(String[] args) {
timer.schedule(new Task(), 0, 1000);
}
}Top
6 楼power_zh(专门在技术区灌水赚分)回复于 2004-09-08 21:20:46 得分 0
upTop




