Android Open Source - MathApp Callback Thread






From Project

Back to project page MathApp.

License

The source code is released under:

MIT License

If you think the Android project MathApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ie.lc.mathApp;
/*from  w  w w  .  ja va  2s.  c o m*/




/**
 * Spawns a thread which executes a callback at a set frequency.
 * @see Callback
 */
public class CallbackThread
{
  private Thread   thread;
  private Callback callback;
  private long    interval;
  private boolean  threadExecute;
  
  
  
  
  
  /**
   * Starts a callback thread and runs it immediately.
   * @param frequencyMillisecs How often to execute the callback.
   * @param callback The callback to execute.
   */
  public CallbackThread( long frequencyMillisecs, Callback callback ) {
    this.callback      = callback;
    this.interval      = frequencyMillisecs;
    this.threadExecute = true;
    
    this.thread = new Thread() {
      public void run() {
        threadLoop();
      }
    };
    
    thread.start();
  }
  
  
  
  
  
  /**
   * Halts callback execution and joins the thread.
   * The calling thread waits until this operation completes.
   */
  public void join() {
    threadExecute = false;
    
    while (thread.isAlive()) {
      try {
        thread.join();
      }
      catch (InterruptedException ex) {
        Thread.interrupted();
      }
    }
  }
  
  
  
  
  
  private void threadLoop() {
    while (threadExecute) {
      callback.execute();
        Util.sleep( interval );
    }
  }
}




Java Source Code List

ie.lc.mathApp.ActivityArithmetic.java
ie.lc.mathApp.ActivityCommonMenu.java
ie.lc.mathApp.ActivityGameBase.java
ie.lc.mathApp.ActivityScore.java
ie.lc.mathApp.ActivitySqrt.java
ie.lc.mathApp.ActivityWave.java
ie.lc.mathApp.CallbackThread.java
ie.lc.mathApp.Callback.java
ie.lc.mathApp.Geo.java
ie.lc.mathApp.Operator.java
ie.lc.mathApp.ScoreData.java
ie.lc.mathApp.Score.java
ie.lc.mathApp.SeekBarAdapter.java
ie.lc.mathApp.Storage.java
ie.lc.mathApp.TextWatcherAdapter.java
ie.lc.mathApp.Util.java
ie.lc.mathApp.Wave.java