
计时器可以使用timer类也可以使用线程类来 *** 作,下面是Thread做的简单的计时器
public class Calculagraph extends Thread {public static void main(String[] args) {
new Calculagraph().start()
}
private long now = 0l
private long start = System.currentTimeMillis()// 程序启动时间的毫秒值
private long time
public void run() {
while (true) {
now = System.currentTimeMillis()// 获取一秒之后的毫秒值
time = now - start// 两个时间相减的到毫秒差
System.out.format("%02d:%02d:%02d\n",
time / (1000 * 60 * 60) % 60/* 时 */,
time / (1000 * 60)% 60/* 分 */,
time / 1000 % 60/* 秒 */)// 格式化字符串输出
try {
Thread.sleep(1000)
} catch (InterruptedException e) {
e.printStackTrace()
}
}
}
}
用java实现一个计时器的方法:
public class TestDingShi implements Runnable
{
Thread xc
Dao dao=new DaoImpl()
public TestDingShi()
{
xc=new Thread(this)//线程开启
xc.start()
}
public void run()
{
while (true)
{
try
{
xc.sleep(1000)//睡眠开始计时
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
}
//TODO定时在此
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)