public static void main(String[] args) {
// 寫一個循環程序,算出計算機一秒鐘的時間內循環的次數。
long i = 0, startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < 1000) {
i++;
}
System.out.println("一秒鐘的時間內循環" + i + "次");
//下面是循環執行時每休眠一毫秒時,每秒鐘for循環可以執行多少次
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
long count=0;
for(;;){
String sentTime1 = sdf.format(new java.util.Date());
try {
Thread.sleep(1L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sentTime2 = sdf.format(new java.util.Date());
if(sentTime1.equals(sentTime2)){
count++;
}else{
System.out.println("一秒鐘循環了:"+count+"次!");
count=0;
}
}
}