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();
}

解决方案 »

  1.   

    public class AddZcm extends Thread
    {
      public AddZcm(){  }
       public static void main(String[] args)
       {   while(true)
           {
           try{
                      System.out.println("hello world!");
             Thread.sleep(1000);
                    }
           }
       }
    }
      

  2.   

    yangjuanli(珂儿) :
        你的程序可能会有问题!
        如果用线程的休眠方法,那么程序表明至少等待1秒才能运行,但并不表明1秒后一定是这个线程被唤起继续运行,如果操作系统内有其它的线程,也有可能是它们被唤起,然后才是这个程序执行。
        个人意见,大家讨论!
      

  3.   

    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);
      

  4.   

    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);
    }
    }