java for 一秒钟可以循环多少次

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;
			}
		}
	}

猜你喜欢

转载自blog.csdn.net/DreamWeaver_zhou/article/details/87276765