Android Open Source - Schooner-3D Looping Thread






From Project

Back to project page Schooner-3D.

License

The source code is released under:

Apache License

If you think the Android project Schooner-3D 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

/*
 * Copyright 2012 Dan Mercer//from w w  w  .j  a v a  2  s.c  o  m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.supermercerbros.gameengine.util;

/**
 * This is a Thread subclass that provides looping functionality. The
 * {@link #loop()} method is called repeatedly until {@link #end()} is called. A
 * LoopingThread can also be paused with {@link #pause()} and
 * {@link #resumeLooping()}.
 */
public abstract class LoopingThread extends Thread {
  private volatile Toggle paused = new Toggle(false);
  protected volatile boolean started = false;
  private volatile boolean ending = false;
  private boolean intermittent;

  // Constructors (these just go through to Thread's constructors):

  public LoopingThread() {
    super();
  }
  public LoopingThread(Runnable runnable) {
    super(runnable);
  }
  public LoopingThread(String threadName) {
    super(threadName);
  }
  public LoopingThread(Runnable runnable, String threadName) {
    super(runnable, threadName);
  }
  public LoopingThread(ThreadGroup group, Runnable runnable) {
    super(group, runnable);
  }
  public LoopingThread(ThreadGroup group, String threadName) {
    super(group, threadName);
  }
  public LoopingThread(ThreadGroup group, Runnable runnable, String threadName) {
    super(group, runnable, threadName);
  }
  public LoopingThread(ThreadGroup group, Runnable runnable,
      String threadName, long stackSize) {
    super(group, runnable, threadName, stackSize);
  }

  // Methods:

  /**
   * Terminates the LoopingThread.
   */
  public void end() {
    if (started) {
      ending = true;
      interrupt();
    }
  }

  /**
   * @return true if {@link #end()} has been called.
   */
  public boolean isEnding() {
    return ending;
  }

  /**
   * Tells the LoopingThread to pause processing. Used with
   * {@link #resumeLooping()}.
   */
  public void pause() {
    synchronized (paused) {
      paused.setState(true);
    }
  }

  /**
   * Tells the LoopingThread to resume processing.
   */
  public void resumeLooping() {
    synchronized (paused) {
      paused.setState(false);
      paused.notify();
    }
  }

  /**
   * Do not call this method. Call {@link #start()} to start the
   * LoopingThread.
   * 
   * @see java.lang.Thread#run()
   */
  @Override
  public void run() {
    if (Thread.currentThread() != this) {
      throw new UnsupportedOperationException(
          "Do not call LoopingThread.run()");
    }
    onBegin();
    while (!ending) {
      loop();
      if (intermittent) {
        pause();
      }

      if (ending) {
        break;
      }
      final boolean isPaused = paused.getState();
      if (isPaused && !ending) {
        onPause();
        waitOnToggle(paused, false);
        onResume();
      }
    }
    onEnd();

    // Thread ends

  }

  /**
   * Waits for the given {@link Toggle} to have the desired state. Breaks out
   * of the wait if {@link #end()} is called.
   */
  protected void waitOnToggle(final Toggle t, final boolean desired) {
    synchronized (t) {
      while (t.getState() != desired) {
        try {
          t.wait();
        } catch (InterruptedException e) {
          if (ending) {
            break;
          }
        }
      }
    }
  }

  /**
   * Waits for the given amount of time
   * 
   * @param millis
   */
  protected void waitForTime(long millis) {
    final long destinationTime = System.currentTimeMillis() + millis;
    synchronized (this) {
      while (System.currentTimeMillis() < destinationTime) {
        try {
          this.wait(destinationTime - System.currentTimeMillis());
        } catch (InterruptedException e) {
          if (ending) {
            break;
          }
        }
      }
    }
  }

  /**
   * This method is called when the Thread begins.
   * Override it to do something at that point.
   */
  protected void onBegin() {
    // This is overridden by subclasses
  }

  /**
   * This method is called when the Thread pauses.
   * Override it to do something at that point.
   */
  protected void onPause() {
    // This is overridden by subclasses
  }
  
  /**
   * This method is called when the Thread resumes after being paused.
   * Override it to do something at that point.
   */
  protected void onResume() {
    // This is overridden by subclasses
  }
  
  /**
   * This method is called when the Thread ends.
   * Override it to do something at that point.
   */
  protected void onEnd() {
    // This is overridden by subclasses
  }

  /**
   * Use this method (<b>not</b> {@link #run()}) to start the LoopingThread.
   */
  @Override
  public void start() {
    started = true;
    super.start();
  }

  /**
   * If true, sets this LoopingThread to be intermittent; in other words, it
   * {@link #pause()}s itself after every call to {@link #loop()}.
   * 
   * @param a
   *            True if this LoopingThread should be intermittent.
   */
  protected void setIntermittent(boolean a) {
    intermittent = a;
  }

  /**
   * @return true if this LoopingThread has been started.
   */
  public boolean hasBeenStarted() {
    return started;
  }

  /**
   * This method is called repeatedly by {@link #run()} until the Thread is
   * paused or ended.
   */
  protected abstract void loop();
}




Java Source Code List

com.supermercerbros.gameengine.GameActivity.java
com.supermercerbros.gameengine.GameView.java
com.supermercerbros.gameengine.Schooner3D.java
com.supermercerbros.gameengine.TestActivity.java
com.supermercerbros.gameengine.TestMaterials.java
com.supermercerbros.gameengine.TestObjects.java
com.supermercerbros.gameengine.animation.AnimationData.java
com.supermercerbros.gameengine.animation.Keyframe.java
com.supermercerbros.gameengine.animation.MeshAnimation.java
com.supermercerbros.gameengine.armature.ActionData.java
com.supermercerbros.gameengine.armature.Action.java
com.supermercerbros.gameengine.armature.BinarySkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Bone.java
com.supermercerbros.gameengine.armature.SkeletalVertexModifier.java
com.supermercerbros.gameengine.armature.Skeleton.java
com.supermercerbros.gameengine.collision.Bounds.java
com.supermercerbros.gameengine.collision.Collider.java
com.supermercerbros.gameengine.collision.CollisionDetector.java
com.supermercerbros.gameengine.collision.Collision.java
com.supermercerbros.gameengine.collision.DebugListener.java
com.supermercerbros.gameengine.collision.Edge.java
com.supermercerbros.gameengine.collision.Face.java
com.supermercerbros.gameengine.collision.Feature.java
com.supermercerbros.gameengine.collision.Intersection.java
com.supermercerbros.gameengine.collision.Line.java
com.supermercerbros.gameengine.collision.LocalDistMinimum.java
com.supermercerbros.gameengine.collision.Matrix.java
com.supermercerbros.gameengine.collision.OnCollisionCheckFinishedListener.java
com.supermercerbros.gameengine.collision.Plane.java
com.supermercerbros.gameengine.collision.Point.java
com.supermercerbros.gameengine.collision.Polyhedron.java
com.supermercerbros.gameengine.collision.SphereBounds.java
com.supermercerbros.gameengine.collision.Vector.java
com.supermercerbros.gameengine.collision.Vertex.java
com.supermercerbros.gameengine.debug.JankCatcher.java
com.supermercerbros.gameengine.debug.LoopLog.java
com.supermercerbros.gameengine.engine.Camera.java
com.supermercerbros.gameengine.engine.DataPipe.java
com.supermercerbros.gameengine.engine.EGLContextLostHandler.java
com.supermercerbros.gameengine.engine.Engine.java
com.supermercerbros.gameengine.engine.GameRenderer.java
com.supermercerbros.gameengine.engine.Light.java
com.supermercerbros.gameengine.engine.Normals.java
com.supermercerbros.gameengine.engine.RenderData.java
com.supermercerbros.gameengine.engine.Scene.java
com.supermercerbros.gameengine.engine.Time.java
com.supermercerbros.gameengine.engine.shaders.Material.java
com.supermercerbros.gameengine.engine.shaders.Program.java
com.supermercerbros.gameengine.engine.shaders.ShaderLib.java
com.supermercerbros.gameengine.engine.shaders.Shader.java
com.supermercerbros.gameengine.engine.shaders.VertexModifier.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteDispatcher.java
com.supermercerbros.gameengine.handlers.OnAnimationCompleteListener.java
com.supermercerbros.gameengine.hud.CoordsConverter.java
com.supermercerbros.gameengine.hud.GameHud.java
com.supermercerbros.gameengine.hud.HudElement.java
com.supermercerbros.gameengine.material.CelShadedMaterial.java
com.supermercerbros.gameengine.material.TexturedMaterial.java
com.supermercerbros.gameengine.math.BezierCurve.java
com.supermercerbros.gameengine.math.Curve.java
com.supermercerbros.gameengine.math.MatrixUtils.java
com.supermercerbros.gameengine.math.Quaternion.java
com.supermercerbros.gameengine.motion.CurveMovement.java
com.supermercerbros.gameengine.motion.MovementData.java
com.supermercerbros.gameengine.motion.Movement.java
com.supermercerbros.gameengine.objects.AnimatedMeshObject.java
com.supermercerbros.gameengine.objects.BasicMaterial.java
com.supermercerbros.gameengine.objects.BonedObject.java
com.supermercerbros.gameengine.objects.GameObject.java
com.supermercerbros.gameengine.objects.Metadata.java
com.supermercerbros.gameengine.parsers.ConstantCurve.java
com.supermercerbros.gameengine.parsers.GameFactory.java
com.supermercerbros.gameengine.parsers.PreBoneData.java
com.supermercerbros.gameengine.parsers.PreObjectData.java
com.supermercerbros.gameengine.parsers.Sch3D.java
com.supermercerbros.gameengine.render.Compositor.java
com.supermercerbros.gameengine.shaders.ProgramSource.java
com.supermercerbros.gameengine.texture.BitmapTexture.java
com.supermercerbros.gameengine.texture.ETC1CompressedTexture.java
com.supermercerbros.gameengine.texture.Texture.java
com.supermercerbros.gameengine.util.BetterDataInputStream.java
com.supermercerbros.gameengine.util.DelayedRunnable.java
com.supermercerbros.gameengine.util.GLES2.java
com.supermercerbros.gameengine.util.IPO.java
com.supermercerbros.gameengine.util.LoopingThread.java
com.supermercerbros.gameengine.util.Toggle.java
com.supermercerbros.gameengine.util.Utils.java