fixeddelayloopgovernor.java

来自「CroftSoft Code Library是一个开源的可移植的纯Java游戏库」· Java 代码 · 共 81 行

JAVA
81
字号
     package com.croftsoft.core.util.loop;

     import com.croftsoft.core.math.MathConstants;

     /*********************************************************************
     * Maintains a periodic rate by stalling the loop by a fixed delay.
     *
     * @author
     *   <a href="http://www.croftsoft.com/">David Wallace Croft</a>
     * @version
     *   2003-05-22
     * @since
     *   2002-03-07
     *********************************************************************/

     public final class  FixedDelayLoopGovernor
       implements LoopGovernor
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     {

     private final long  delayMillis;

     private final int   delayNanos;

     //////////////////////////////////////////////////////////////////////
     // constructor methods
     //////////////////////////////////////////////////////////////////////

     public  FixedDelayLoopGovernor (
       long  delayMillis,
       int   delayNanos )
     //////////////////////////////////////////////////////////////////////
     {
       this.delayMillis = delayMillis;

       this.delayNanos  = delayNanos;
     }

     /*********************************************************************
     * Constructs a LoopGovernor with the specified target frequency.
     *
     * @param  frequency
     *
     *   The targeted loop frequency in loops per second.
     *********************************************************************/
     public  FixedDelayLoopGovernor ( double  frequency )
     //////////////////////////////////////////////////////////////////////
     {
       if ( frequency <= 0.0 )
       {
         throw new IllegalArgumentException ( "frequency <= 0.0" );
       }

       long  periodNanos         = ( long ) ( MathConstants.NANOSECONDS_PER_SECOND / frequency );

       delayMillis         = periodNanos / MathConstants.NANOSECONDS_PER_MILLISECOND;

       delayNanos = ( int )         ( periodNanos % MathConstants.NANOSECONDS_PER_MILLISECOND );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     /*********************************************************************
     * Stalls the current Thread by the fixed delay period.
     *********************************************************************/
     public void  govern ( )
       throws InterruptedException
     //////////////////////////////////////////////////////////////////////
     {
       Thread.sleep ( delayMillis, delayNanos );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?