用JAVA怎么使变量每秒钟加1??急!!
来源:百度知道 编辑:UC知道 时间:2024/06/30 21:28:31
就是一个COUNT
使它每秒钟加一
怎么实现啊?
使它每秒钟加一
怎么实现啊?
其实就是让线程休眠一秒就可以了很简单
例子如下
package com.zhh.test;
public class Count {
public static void main(String[] args){
int count = 0;
while(true){
System.out.println(count++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
每秒都要加,当然就最好要单独的线程来跑了。
一楼可以了~
package com.myh;
import java.util.Timer;
import java.util.TimerTask;
public class jia1 {
/**
* @param args
*/
public static void main(String[] args) {
TimerTask task = new TimerTask() {
int count = 0;
public void run() {
count++;
System.out.println(count);
}
};
Timer t = new Timer();
t.schedule(task, 0, 1000);
}
}
----------------