[java]代码库
public class ThreadTest extends HttpServlet implements Runnable {
public void run() {
try {
Calendar c;
while(true){
c=Calendar.getInstance();//时间对象
int mm=30;//默认线程间隔时间
System.out.println("mm:"+mm+" time:"+c.get(Calendar.SECOND));
//初始第一次的间隔时间
if(c.get(Calendar.SECOND)%30!=0)
{
System.out.println("第一次间隔时间设置");
mm=60-c.get(Calendar.SECOND);
}
System.out.println("mm:"+mm);
Thread.sleep(mm*1000L);
if(c.get(Calendar.SECOND)==0)//条件执行
System.out.println("run Thread");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
System.out.println("------------Thread init()-------------");
ThreadTest t=new ThreadTest();
new Thread(t).start();
}
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
System.out.println(c.get(Calendar.SECOND));
ThreadTest t=new ThreadTest();
new Thread(t).start();
}
}
//源代码片段来自云代码http://yuncode.net