long time = -1;
while (true) {
Thread.sleep(1000);
if (time == -1)
time = System.currentTimeMillis();
else
time += 1000;
System.out.println("Curernt time is:" + format.format(new Date(time)));
}
不是高手, 试试而已. 可以吗?
while (true) {
Thread.sleep(1000);
if (time == -1)
time = System.currentTimeMillis();
else
time += 1000;
System.out.println("Curernt time is:" + format.format(new Date(time)));
}
不是高手, 试试而已. 可以吗?
解决方案 »
- 我们正学JDBC呢,可是我觉得书讲得太深太少了,请问谁能给我推荐几个介绍这方面的电子书
- spring中注入接口,配置文件该怎么写啊?
- 大家遇到过这个问题吗?
- sqlMap iterate
- 新手的困惑
- 问个很菜的问题,服务器之间是怎么传递xml文件的呢?
- 你们老说的CMP/BMP是什么啊,我只听说3类EJB啊
- 这是什么意思import MyWindowListener;?
- 寻找“java数据库应用程序编程指南”的原代码的下载。
- Maven导入项目时报错!
- 为什么我读不了CSDN首页的RSS
- 老是出现问题:javax.servlet.ServletException:java.lang.ClassCastException
忘了这句
哎,Timer类居然无法用,非要逼着用线程,
可悲啊.....
import java.util.Timer;
import java.util.TimerTask;public class TimerDemo { public static void main(String[] args) {
Timer timer = null;
try {
timer = new Timer(true);
timer.schedule(new TestTask(), 0, 1000);
while (true);
} finally {
timer.cancel();
}
}
}class TestTask extends TimerTask {
public void run() {
System.out.println(Calendar.getInstance().getTime().toString());
}
}愿意的话,自己还可以添加结束循环的条件
import java.util.TimerTask;/**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/public class Reminder {
Timer timer; public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
} class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
timer.cancel(); //Terminate the timer thread
}
} public static void main(String args[]) {
System.out.println("About to schedule task.");
new Reminder(5);
System.out.println("Task scheduled.");
}
}
public class AnnoyingBeep {
Toolkit toolkit;
Timer timer; public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
} class RemindTask extends TimerTask {
int numWarningBeeps = 3; public void run() {
if (numWarningBeeps > 0) {
toolkit.beep();
System.out.println("Beep!");
numWarningBeeps--;
} else {
toolkit.beep();
System.out.println("Time's up!");
//timer.cancel(); //Not necessary because we call System.exit
System.exit(0); //Stops the AWT thread (and everything else)
}
}
}
...
}
public class AnnoyingBeep {
Toolkit toolkit;
Timer timer; public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
} class RemindTask extends TimerTask {
int numWarningBeeps = 3; public void run() {
if (numWarningBeeps > 0) {
toolkit.beep();
System.out.println("Beep!");
numWarningBeeps--;
} else {
toolkit.beep();
System.out.println("Time's up!");
//timer.cancel(); //Not necessary because we call System.exit
System.exit(0); //Stops the AWT thread (and everything else)
}
}
}
}
看看楼主的名字 JuckFava (hehe)
你又没有说每隔一秒钟要处理一些事务.只是打印而已, 写个死循环就可以了. 任何语言都很简单吧.