当前位置: 技术问答>java相关
我想在java程序中加一个定时器
来源: 互联网 发布时间:2017-03-19
本文导语: 我想在java程序中加一个定时器每隔30秒执行一次,如何实现急急急急 | import java.util.*; public class TimerTest { public TimerTest() { } public static void main(String argv[]) { try { ...
我想在java程序中加一个定时器每隔30秒执行一次,如何实现急急急急
|
import java.util.*;
public class TimerTest {
public TimerTest() { }
public static void main(String argv[]) {
try {
Timer timer = new Timer();
job j = new job();
// 每 3 秒执行一次 job 里面的run。
timer.scheduleAtFixedRate(j, new Date(), 3000l);
int i = 0;
while (true) {
// 先睡 2 秒再说……
Thread.sleep(3000l);
i++;
System.out.println("我正在休息……");
if (i==5) {
// 停止记时
timer.cancel();
System.out.println("取消定时器的工作。");
}
//中断 while 循环
if (i==7)
break;
}
} catch(Exception e) {
System.out.println(e.toString());
}
}
}
class job extends TimerTask {
public void run() {
System.out.println("定时器正在运行,执行相关的任务……");
}
}
public class TimerTest {
public TimerTest() { }
public static void main(String argv[]) {
try {
Timer timer = new Timer();
job j = new job();
// 每 3 秒执行一次 job 里面的run。
timer.scheduleAtFixedRate(j, new Date(), 3000l);
int i = 0;
while (true) {
// 先睡 2 秒再说……
Thread.sleep(3000l);
i++;
System.out.println("我正在休息……");
if (i==5) {
// 停止记时
timer.cancel();
System.out.println("取消定时器的工作。");
}
//中断 while 循环
if (i==7)
break;
}
} catch(Exception e) {
System.out.println(e.toString());
}
}
}
class job extends TimerTask {
public void run() {
System.out.println("定时器正在运行,执行相关的任务……");
}
}
|
import java.nio.channels.*;
import java.io.*;
import java.util.*;
public class test
{
public void demo() throws Exception
{
long delay = 8000;
java.util.Timer t = new java.util.Timer();
try
{
t.schedule(new TimerTask(){
public void run()//这里面加上你要干的事情
{
//System.out.println("go to play cs");
}
},delay); //delay是你要设定的时间间隔
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
private static void loopTime() throws Exception
{
long delay = 8000;
java.util.Date thisTime=new java.util.Date();
java.util.Date lastTime=new java.util.Date();
try
{
lastTime.setTime(System.currentTimeMillis());
while (true)
{
try
{
thisTime.setTime(System.currentTimeMillis());
Thread.sleep(delay);
//调用定时任务
//这里加上要干的事情
System.out.println("go to play cs again");
lastTime.setTime(thisTime.getTime());
}
catch (InterruptedException e)
{
}
}
}
catch(Exception e)
{
}
}
public static void main(String args[])
{
test a=new test();
try{
a.demo();
a.loopTime();
}
catch(Exception e)
{
}
}
}
这程序中的两个方法都能实现你想要的功能,不过程序运行时,只需要一个就可以了。你可以将我的代码编译运行,结果如下:
go to play cs //只会显示一次,因为sleep时进入了下一个方法,回不来了
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
。。。。。。。。。
。。。。。。。。。
import java.io.*;
import java.util.*;
public class test
{
public void demo() throws Exception
{
long delay = 8000;
java.util.Timer t = new java.util.Timer();
try
{
t.schedule(new TimerTask(){
public void run()//这里面加上你要干的事情
{
//System.out.println("go to play cs");
}
},delay); //delay是你要设定的时间间隔
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
private static void loopTime() throws Exception
{
long delay = 8000;
java.util.Date thisTime=new java.util.Date();
java.util.Date lastTime=new java.util.Date();
try
{
lastTime.setTime(System.currentTimeMillis());
while (true)
{
try
{
thisTime.setTime(System.currentTimeMillis());
Thread.sleep(delay);
//调用定时任务
//这里加上要干的事情
System.out.println("go to play cs again");
lastTime.setTime(thisTime.getTime());
}
catch (InterruptedException e)
{
}
}
}
catch(Exception e)
{
}
}
public static void main(String args[])
{
test a=new test();
try{
a.demo();
a.loopTime();
}
catch(Exception e)
{
}
}
}
这程序中的两个方法都能实现你想要的功能,不过程序运行时,只需要一个就可以了。你可以将我的代码编译运行,结果如下:
go to play cs //只会显示一次,因为sleep时进入了下一个方法,回不来了
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
go to play cs again
。。。。。。。。。
。。。。。。。。。