我希望我的int time = 0
每秒增加1,能够暂停和恢复。
答案 0 :(得分:1)
对于计时器从这里开始java timers tutorial给它一个裂缝并在遇到问题时再问一个问题。
答案 1 :(得分:0)
final AtomicLong i = new AtomicLong(0);
Thread th = new Thread() {
@Override
public void run() {
try {
while (true) {
long lastSeconds = System.currentTimeMillis() / 1000;
sleep(100);
long delta = System.currentTimeMillis() / 1000 - lastSeconds;
i.getAndAdd(delta);
if (delta > 0)
System.out.println(i.get());
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
};
th.start();