当前位置: 技术问答>java相关
java中有没有计时器?
来源: 互联网 发布时间:2015-06-09
本文导语: java中有没有计时器? 或者有没有让系统延时的方法?(不是在awt中) | swing Timer or Thread | 在java中,一般用来计时的方法是两个System.currentTimeMillis()相减,便可以得出所消耗的时间. 至于延时,...
java中有没有计时器?
或者有没有让系统延时的方法?(不是在awt中)
或者有没有让系统延时的方法?(不是在awt中)
|
swing Timer
or
Thread
or
Thread
|
在java中,一般用来计时的方法是两个System.currentTimeMillis()相减,便可以得出所消耗的时间.
至于延时,我好像没有听说过
至于延时,我好像没有听说过
|
我在看《JAVA编程思想》的多线程一章中好象有!自己找找吧!
|
使用java.awt.Robot类有一个delay方法,
详情请看我的java技术专栏
www.csdn.net/Author/turbochen
中的
详情请看我的java技术专栏
www.csdn.net/Author/turbochen
中的
|
java.util.timer, go there and have a look
|
没有专门的定时器,可以自己实现:
//define
interface TimerAble
{
void onTimer();
}
class Timer extends Thread
{
private TimerAble ta;
private long interval;
private boolean stop = false;
public Timer( TimerAble ta,long inerval )
{
this.ta = ta;
this.inerval = inerval;
}
protected void run()
{
while( stop != true )
{
ta.onTimer();
Thread.sleep( interval );
}
}
public void stop()
{
this.stop = true;
}
public void start()
{
this.stop = false;
super.start();
}
}
class MyApp extends Object implements TimerAble
{
protected void onTimer()
{
//The code want to run on timer.
}
public static void main( String[] args )
{
MyApp app = new MyApp()
Timer t = new Timer( app,1000 )
t.start();//begin timer
//......
t.stop;//stop timer
}
}
//define
interface TimerAble
{
void onTimer();
}
class Timer extends Thread
{
private TimerAble ta;
private long interval;
private boolean stop = false;
public Timer( TimerAble ta,long inerval )
{
this.ta = ta;
this.inerval = inerval;
}
protected void run()
{
while( stop != true )
{
ta.onTimer();
Thread.sleep( interval );
}
}
public void stop()
{
this.stop = true;
}
public void start()
{
this.stop = false;
super.start();
}
}
class MyApp extends Object implements TimerAble
{
protected void onTimer()
{
//The code want to run on timer.
}
public static void main( String[] args )
{
MyApp app = new MyApp()
Timer t = new Timer( app,1000 )
t.start();//begin timer
//......
t.stop;//stop timer
}
}
|
没有专门的定时器,可以自己实现:
//define
interface TimerAble
{
void onTimer();
}
class Timer extends Thread
{
private TimerAble ta;
private long interval;
private boolean stop = false;
public Timer( TimerAble ta,long inerval )
{
this.ta = ta;
this.inerval = inerval;
}
protected void run()
{
while( stop != true )
{
ta.onTimer();
Thread.sleep( interval );
}
}
public void stop()
{
this.stop = true;
}
public void start()
{
this.stop = false;
super.start();
}
}
class MyApp extends Object implements TimerAble
{
protected void onTimer()
{
//The code want to run on timer.
}
public static void main( String[] args )
{
MyApp app = new MyApp()
Timer t = new Timer( app,1000 )
t.start();//begin timer
//......
t.stop;//stop timer
}
}
//define
interface TimerAble
{
void onTimer();
}
class Timer extends Thread
{
private TimerAble ta;
private long interval;
private boolean stop = false;
public Timer( TimerAble ta,long inerval )
{
this.ta = ta;
this.inerval = inerval;
}
protected void run()
{
while( stop != true )
{
ta.onTimer();
Thread.sleep( interval );
}
}
public void stop()
{
this.stop = true;
}
public void start()
{
this.stop = false;
super.start();
}
}
class MyApp extends Object implements TimerAble
{
protected void onTimer()
{
//The code want to run on timer.
}
public static void main( String[] args )
{
MyApp app = new MyApp()
Timer t = new Timer( app,1000 )
t.start();//begin timer
//......
t.stop;//stop timer
}
}
|
刚才才了一下document
jdk1.3以后支持这个操作,实现原理和我上面写的差不错。
java.util.Timer; = Timer
java.util.TimerTask; = TimerAble
jdk1.3以后支持这个操作,实现原理和我上面写的差不错。
java.util.Timer; = Timer
java.util.TimerTask; = TimerAble