int runTime=1000*5; int FrameRate=1000/6; int checkTime=FrameRate; long startTime=new Date().getTime(); while(runTime>checkTime) { //checkValue=moveVector3(90, checkValue); //checkValue.print(); checkTime+=FrameRate; System.out.println(checkTime); try { Thread.sleep(FrameRate); } catch (InterruptedException e) { e.printStackTrace(); } } long endTime=new Date().getTime(); System.out.println(endTime-startTime);
修正延时
public static void main(String[] args) { int runTime=1000; int FrameRate=1000/6; int checkTime=FrameRate; long startTime,entTime; long checkSTime=new Date().getTime(); int i=0; while(runTime>checkTime) { System.out.println(i); startTime =new Date().getTime(); try { Thread.sleep(300); } catch (InterruptedException e1) { e1.printStackTrace(); } entTime=new Date().getTime(); long check=entTime-startTime; if(check<FrameRate) checkTime+=FrameRate; else checkTime+=check; i++; try { if(check<FrameRate) Thread.sleep(FrameRate-check); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("time : " +( new Date().getTime()-checkSTime)); }