⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 looper.java

📁 Java游戏高级编程!!很不错的!!!Java游戏高级编程!!很不错的
💻 JAVA
字号:
     package com.croftsoft.core.util.loop;

     import com.croftsoft.core.lang.NullArgumentException;
     import com.croftsoft.core.lang.ex.ExceptionHandler;
     import com.croftsoft.core.lang.lifecycle.Lifecycle;
     /*********************************************************************
     * Periodically runs a task in a loop using a separate thread.
     *
     * @author
     *   <a href="http://www.croftsoft.com/">David Wallace Croft</a>
     * @version
     *   2003-05-22
     * @since
     *   2000-04-27
     *********************************************************************/

     public final class  Looper       implements Lifecycle
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     {     private final Loopable          loopable;     private final ExceptionHandler  exceptionHandler;     private final String            threadName;     private final int               threadPriority;     private final boolean           useDaemonThread;     //

     private LoopGovernor  loopGovernor;
     private Thread        thread;     private boolean       stopRequested;

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

     /*********************************************************************
     * Main constructor.
     *
     * <p>
     * Call the start() method after construction to begin looping.     * </p>
     *
     * @param  loopable
     *
     *   The loop() method of this object will be called periodically.
     *   Looping will stop if this method returns false.
     *
     * @param  exceptionHandler
     *
     *   Handles all Exceptions generated by runnable.run().
     *   The handleException() Object argument passed will be the loopable.
     *   If null, the default behavior of printing any Exception to the
     *   standard error output and stopping looping will be used.
     *********************************************************************/
     public  Looper (
       Loopable          loopable,       LoopGovernor      loopGovernor,
       ExceptionHandler  exceptionHandler,       String            threadName,       int               threadPriority,       boolean           useDaemonThread )
     //////////////////////////////////////////////////////////////////////
     {       NullArgumentException.check ( this.loopable = loopable );       setLoopGovernor ( loopGovernor );
       this.exceptionHandler = exceptionHandler;       this.threadName       = threadName;
       this.threadPriority   = threadPriority;       this.useDaemonThread  = useDaemonThread;
     }

     public  Looper ( Loopable  loopable )     //////////////////////////////////////////////////////////////////////
     {       this (         loopable,         new FixedDelayLoopGovernor ( 0L, 0 ),         ( ExceptionHandler ) null,         ( String ) null,         Thread.MIN_PRIORITY,         true );     }     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////     public void  setLoopGovernor ( LoopGovernor  loopGovernor )     //////////////////////////////////////////////////////////////////////
     {       NullArgumentException.check ( this.loopGovernor = loopGovernor );     }
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public void  init ( )     //////////////////////////////////////////////////////////////////////
     {     }

     public synchronized void  start ( )     //////////////////////////////////////////////////////////////////////     {       stopRequested = false;       if ( thread == null )       {         Runnable  runnable =           new Runnable ( )           {             public void  run ( )             {               loop ( );             }           };         if ( threadName == null )         {           thread = new Thread ( runnable );         }         else         {           thread = new Thread ( runnable, threadName );         }         thread.setPriority ( threadPriority );         thread.setDaemon ( useDaemonThread );         thread.start ( );       }       else       {         notify ( );       }     }
     public synchronized void  stop ( )     //////////////////////////////////////////////////////////////////////     {       stopRequested = true;       thread.interrupt ( );     }     public synchronized void  destroy ( )     //////////////////////////////////////////////////////////////////////     {       thread = null;       stopRequested = false;       notify ( );     }

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

     private void  loop ( )     //////////////////////////////////////////////////////////////////////     {       while ( thread != null )       {         try         {           if ( loopable.loop ( ) )
           {             loopGovernor.govern ( );           }           else           {             stopRequested = true;
           }         }         catch ( InterruptedException  ex )         {         }         catch ( Exception  ex )
         {
           if ( ( exceptionHandler == null )
             || !exceptionHandler.handleException ( ex, loopable ) )
           {
             stopRequested = true;
             ex.printStackTrace ( );
           }
         }         if ( stopRequested )         {           synchronized ( this )           {             while ( stopRequested )             {               try               {                 wait ( );               }               catch ( InterruptedException  ex )               {               }             }           }         }       }     }

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

⌨️ 快捷键说明

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