timer example...Post here...

package com.tiantian.servlet;

import com.tiantian.pub.MyTask;
import javax.servlet.*;
import javax.servlet.http.*;

public class TimerListener extends HttpServlet implements
        ServletContextListener {

    private java.util.Timer timer = null;

    public void contextInitialized(ServletContextEvent event) {
        timer = new java.util.Timer(true);
        timer.schedule(new MyTask(event.getServletContext()), 0, 1000 * 10);
    }

    public void contextDestroyed(ServletContextEvent event) {
        timer.cancel();
    }
    }




package com.tiantian.pub;

import java.util.TimerTask;
import javax.servlet.ServletContext;

public class MyTask extends TimerTask {

    private ServletContext context = null;

    public MyTask(ServletContext context) {
        this.context = context;
    }

    public void run() {
        context.log("RUNNING..." + System.currentTimeMillis());
    }
}


1s=1000ms...
if u encountered puzzle, ask reference for advice.

你可能感兴趣的:(java,servlet)