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();
}
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();
}
解决方案 »
- 用Spring不能再用new了?
- hibernate映射自动获取时间的问题
- 正则表达式问题?
- jboss启动:org.hibernate.MappingException: entity class not found:
- struts,servlet 中各用什么方法实现跳转,各有哪些参数
- 救命~~同一个htm,直接执行就没问题,放tomcat下就报错
- 请问hibernate配置!
- 大家见过这个错误吗
- 急问:sql server 2000 Error establishing socket 错误??
- ActiveMQ,两个系统间如何数据交互
- 高分请各位大侠架构系统!
- 麻烦各位了~帮忙解决解决吧!!
{
public AddZcm(){ }
public static void main(String[] args)
{ while(true)
{
try{
System.out.println("hello world!");
Thread.sleep(1000);
}
}
}
}
你的程序可能会有问题!
如果用线程的休眠方法,那么程序表明至少等待1秒才能运行,但并不表明1秒后一定是这个线程被唤起继续运行,如果操作系统内有其它的线程,也有可能是它们被唤起,然后才是这个程序执行。
个人意见,大家讨论!
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);
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);
}
}