
正文
java循环定时器代码 java定时循环执行
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用java编写一个定时器每隔一秒钟,在控制台打印出
看看下面代码,使用的是java.util.Timer类
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class T {
public static void main(String []args){
Timer timer=new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() {
System.out.println(new Date());
}
}, 0,1000);
}
}
相关问答
Q1: java代码启动一个定时任务
在应用里经常都有用到在后台跑定时任务的需求。举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据、文件等。在本文里,3种不同的实现方法:
普通thread实现
TimerTask实现
ScheduledExecutorService实现
1.普通thread
这是最常见的,创建一个thread,然后让它在while循环里一直运行着,通过sleep方法来达到定时任务的效果。这样可以快速简单的实现,代码如下:
public class Task1 {
public static void main(String[] args) {
// run in a second
final long timeInterval = 1000;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
// ------- code for task to run
System.out.println("Hello !!");
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
2.用Timer和TimerTask
上面的实现是非常快速简便的,但它也缺少一些功能。
用Timer和TimerTask的话与上述方法相比有如下好处:
当启动和去取消任务时可以控制
第一次执行任务时可以指定你想要的delay时间
在实现时,Timer类可以调度任务,TimerTask则是通过在run()方法里实现具体任务。
Timer实例可以调度多任务。
当Timer的构造器被调用时,创建了一个线程,这个线程可以用来调度任务:
import java.util.Timer;
import java.util.TimerTask;
public class Task2 {
public static void main(String[] args) {
TimerTask task = new TimerTask() {
@Override
public void run() {
// task to run goes here
System.out.println("Hello !!!");
}
};
Timer timer = new Timer();
long delay = 0;
long intevalPeriod = 1 * 1000;
// schedules the task to be run in an interval
timer.scheduleAtFixedRate(task, delay,
intevalPeriod);
} // end of main
}
3.ScheduledExecutorService
ScheduledExecutorService是从Java SE 5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。
相比于上两个方法,它有以下好处:
相比于Timer的单线程,它是通过线程池的方式来执行任务的
可以很灵活的去设定第一次执行任务delay时间
提供了良好的约定,以便设定执行的时间间隔
通过ScheduledExecutorService#scheduleAtFixedRate展示这个例子,通过代码里参数的控制,首次执行加了delay时间:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Task3 {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
// task to run goes here
System.out.println("Hello !!");
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
}
}
Q2: java写一个定时器,定时对一个变量赋不同值,这个程序代码怎么写
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class MainEntry {
private int a;
public void setVal(){
Timer timer = new Timer();
//每隔一秒生成一个[1,100)内java循环定时器代码的随机整数java循环定时器代码,赋给成员a
timer.schedule(new TimerTask() {
@Override
public void run() {
Random rand = new Random();
setA(rand.nextInt(100));
}
}, 1000);
}
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
public static void main(String[] args) {
MainEntry me = new MainEntry();
me.setVal();
}
}
Q3: 求一个简单的Java定时器源代码
我是让它每10毫秒扫描一下。
Calendar ca=null;
Calendar ca1=null;
Timer timer=new Timer();
static Connection con=null;
public void chu(){
ca=Calendar.getInstance();
ca1=(Calendar)ca.clone();
ca1.add(Calendar.SECOND, 20);
}
public void Time(){
float s=ca1.get(Calendar.SECOND)-ca.get(Calendar.SECOND)+(ca1.get(Calendar.MILLISECOND)-ca.get(Calendar.MILLISECOND))/1000f;
if(con==null){
System.out.println("耗时"+String.valueOf(s)+"秒");
if(ca.after(ca1)){
System.out.println("取不到连接");
timer.cancel();
return ;
}
}else{
System.out.println("耗时"+String.valueOf(s)+"秒");
}
ca.add(Calendar.MILLISECOND, 10);
timer.schedule(
new TimerTask() {
public void run() {
System.out.println("时间在溜走。。。。");
Time();
}
}, ca.getTime());
}
void getcon(){
Connection con=null;//获取Connection
chu();
Time();
}
关于java循环定时器代码和java定时循环执行的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








