javatimer
发布时间: 2025-01-20 15:55:56
Ⅰ java timer类实现随机时间调用方法
//一秒后开始执行,每五秒内随机执行一次.
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;
public class test1{
public static void main(String[] args) throws Exception{
Timer time=new Timer();
time.schele(new tast(),1000,5000);
}
}
class tast extends TimerTask{
public void run(){
Date date=new Date();
System.out.println("I'm start!"+date.toString());
Random random=new Random();
try{
Thread.sleep(random.nextInt(5000));
}catch(Exception e){
e.printStackTrace();
}
date=new Date();
System.out.println("I'm execute!"+date.toString());
}
}
热点内容