java.util.Timer
java.util.TimerTask
java.util.TimerTask
解决方案 »
- java版本冲突问题
- 大家说说swing把!!!
- 如何儲存jTextPane中的文字入database
- java 压缩 ,想将文件夹内的每个文件单独压缩并命名为原文件名.zip,怎么修改?请指教!
- 怎样获得某年下的所有周
- 急求指教:使用Collections类有什么好处?
- Optimizeit 5.5的全套注册机,十分感谢
- 几道程序设计题目(JAVA)求大神指点
- 请问try ... catch 和 throw有什么区别,谢谢
- 请问谁用javamail作过发送中文html邮件???能否指教。。
- 请问用Calendar 或Date设置的时间怎么样让它写入bios,在重启后也能生效
- 谢谢指点初学者(如何构建一个j2ee开发平台和应用平台)
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
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以后可以自动定期运行的一段程序
时间间隔是毫秒
<listener>
<listener-class>TimerListener</listener-class>
</listener>这样就可以启动了
xx_ch(好客小虫) --能把完整的给我吗,上面的程序好象不全--------, 1000, 时间间隔);