你可以看一下java.util.TimerTask对象的应用!

解决方案 »

  1.   

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

  2.   

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JApplet;
    import javax.swing.Timer;/**
     * Descripton TODO
     * 
     * @author FuYao
     */
    public class TimerTest extends JApplet {    public void init() {
            Timer timer = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    System.out.println("hello");
                }
    });
    timer.start();
        }}
      

  3.   

    import java.util.Timer;
    import java.util.TimerTask;
    /**
     * Descripton TODO
     * 
     * @author FuYao
     */
    public class TimerTest {

    public static void main(String[] args) {

    Timer timer=new Timer();
    timer.scheduleAtFixedRate(new TimerTaskImpl(),1000,1000);

    }}class TimerTaskImpl extends TimerTask { /* (non-Javadoc)
     * @see java.util.TimerTask#run()
     */
    public void run() {
    // TODO Auto-generated method stub
    System.out.println("hello");
    }

    }