java.util.Timer
java.util.TimerTask

解决方案 »

  1.   

    多线程.如果你是windows系统,还可以打包成.exe文件(不用多线程),在计划任务中执行
      

  2.   

    Timer timer=new Timer();
    TimerTask task=new TimerTask(){
    public void run(){
    //here is what you want to do, println helloworld for example
    System.out.println("HelloWorld");
    }
    };
    //period between two execute
    long period=5*1000;
    timer.schedule(task,0,period);
    5秒打印一次helloworld
      

  3.   

    import java.net.URL;
    import java.io.*;
    import java.net.*;public class TimerListener
        implements javax.servlet.ServletContextListener {
      private java.util.Timer timer;
      long iVoxCount = 0;
      long iMsgCount = 0;
      private java.io.InputStream is = null;  public TimerListener() {
        System.out.println("初始化成功");
        timer = new java.util.Timer(true);
      }
      public void contextDestroyed(javax.servlet.ServletContextEvent event) {
        System.out.println("系统关闭");
        timer.cancel();
      }  public void contextInitialized(javax.servlet.ServletContextEvent event) {
        System.out.println("开始检测");
        //System.out.println( event.getServletContext().getRealPath( "/" ) );
        timer.schedule( new java.util.TimerTask() {
          private HttpURLConnection conn;
          public void run() {
           // System.out.println("111");
            try {          URL url = new URL("servletURL");
              try {
                conn = (HttpURLConnection) url.openConnection();
                is = conn.getInputStream();
                is.close();
                conn.disconnect();
              }
              catch (IOException ex1) {
              }
            }
            catch (MalformedURLException ex) {
            }
          }
        }
        , 1000, 时间间隔);
      }
    }不知道这样能满足你的要求不,这是启动tomcat以后可以自动定期运行的一段程序
    时间间隔是毫秒
      

  4.   

    "treeroot(根根)"和"蔼xx_ch(好客小虫)"两种方法分别是如何启动的呢?是写在servlet上的吗?
      

  5.   

    我的意思是如何实现"启动tomcat以后可以自动定期运行的一段程序",就是说这段代码应该放的地方是自己写一个servlet还是其它什么的来启动他.
      

  6.   

    哦,你没用过这个类呀,在web.xml里加上这2句
      <listener>
        <listener-class>TimerListener</listener-class>
      </listener>这样就可以启动了
      

  7.   


        xx_ch(好客小虫) --能把完整的给我吗,上面的程序好象不全--------, 1000, 时间间隔);
      

  8.   

    这个就是全的呀,你放到编译工具里就可以看出来了,那块对应的是timer.schedule这里的括号
      

  9.   

    那这个类应该放到什么地方,就是说xml中读这个类是读什么地方的呢是读servlet还是读bean的呢,不需要填路径吗.