Android Open Source - RZAndroidBaseUtils Threading Utils






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils 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 com.raizlabs.baseutils;
/*w ww. java2 s.  c om*/
import android.os.Handler;
import android.os.Looper;
import android.view.View;

public class ThreadingUtils {

  private static Handler uiHandler;
  /**
   * @return A {@link Handler} that is bound to the UI thread.
   */
  public static Handler getUIHandler() {
    if (uiHandler == null) uiHandler = new Handler(Looper.getMainLooper());
    return uiHandler;
  }
  
  /**
   * Returns true if this function was called on the thread the given
   * {@link Handler} is bound to.
   * @param handler The {@link Handler} to check the thread of.
   * @return True if this function was called on the {@link Handler}'s
   * thread.
   */
  public static boolean isOnHandlerThread(Handler handler) {
    Looper handlerLooper = handler.getLooper();
    if (handlerLooper != null) {
      return handlerLooper.equals(Looper.myLooper());
    }
    
    return false;
  }
  
  /**
   * @return True if this function was called from the UI thread
   */
  public static boolean isOnUIThread() {
    return Looper.getMainLooper().equals(Looper.myLooper());
  }
  

  
  /**
   * Runs the given {@link Runnable} on the thread the given {@link Handler}
   * is bound to. This will execute immediately, before this function returns,
   * if this function was already called on the given {@link Handler}'s thread.
   * Otherwise, the {@link Runnable} will be posted to the {@link Handler}.
   * @param handler The {@link Handler} to run the action on.
   * @param action The {@link Runnable} to execute.
   * @return True if the action was already executed before this funcion
   * returned, or false if the action was posted to be handled later.
   */
  public static boolean runOnHandler(Handler handler, Runnable action) {
    if (isOnHandlerThread(handler)) {
      action.run();
      return true;
    } else {
      handler.post(action);
      return false;
    }
  }
  
  /**
   * Runs the given {@link Runnable} on the UI thread. This will execute
   * immediately, before this function returns, if this function was called
   * on the UI thread. Otherwise, the {@link Runnable} will be posted to the
   * UI thread.
   * @see #runOnUIThread(Runnable, Handler)
   * @see #runOnUIThread(Runnable, View)
   * @param action The {@link Runnable} to execute on the UI thread.
   * @return True if the action was already executed before this function
   * returned, or false if the action was posted to be handled later.
   */
  public static boolean runOnUIThread(Runnable action) {
    if (isOnUIThread()) {
      action.run();
      return true;
    } else {
      getUIHandler().post(action);
      return false;
    }
  }
  
  /**
   * Runs the given {@link Runnable} on the UI thread. This will execute
   * immediately, before this function returns, if this function was called
   * on the UI thread. Otherwise, the {@link Runnable} will be posted using
   * the given {@link View}.
   * <br><br>
   * NOTE: This method will attempt to force the action onto the UI thread.
   * <br><br>
   * WARNING: The action may still not be taken if the view's 
   * {@link View#post(Runnable)} method returns true, but doesn't execute. 
   * (This is the case when the view is not attached to a window). 
   * @see #runOnUIThread(Runnable)
   * @see #runOnUIThread(Runnable, Handler)
   * @param action The {@link Runnable} to execute.
   * @param v A {@link View} to use to post the {@link Runnable} if this
   * wasn't called on the UI thread.
   * @return True if the action was already executed before this function
   * returned, or false if the action was posted.
   */
  public static boolean runOnUIThread(View v, Runnable action) {
    if (isOnUIThread()) {
      action.run();
      return true;
    } else {
      if (!v.post(action)) {
        runOnUIThread(action);
      }
      return false;
    }
  }
  
  /**
   * Runs the given {@link Runnable} on the UI thread. This will execute
   * immediately, before this function returns, if this function was called
   * on the UI thread. Otherwise, the {@link Runnable} will be posted using
   * the given {@link View}.
   * <br><br>
   * NOTE: This method will attempt to force the action onto the UI thread.
   * <br><br>
   * WARNING: The action may still not be taken if the view's 
   * {@link View#post(Runnable)} method returns true, but doesn't execute. 
   * (This is the case when the view is not attached to a window). 
   * @see #runOnUIThread(Runnable)
   * @see #runOnUIThread(Runnable, Handler)
   * @param v A {@link View} to use to post the {@link Runnable} if this
   * wasn't called on the UI thread.
   * @param action The {@link Runnable} to execute.
   * @return True if the action was already executed before this function
   * returned, or false if the action was posted.
   */
  @Deprecated
  public static boolean runOnUIThread(Runnable action, View v) {
    return runOnUIThread(v, action);
  }
  
  /**
   * Runs the given {@link Runnable} immediately if this function is called
   * on the UI thread. Otherwise, it is posted to the given {@link Handler}
   * and executed on its bound thread. Though it is assumed that the given
   * {@link Handler} is bound to the UI thread, it is not necessary, and it
   * will execute the action either way.
   * @param action The {@link Runnable} to execute.
   * @param handler The {@link Handler} to post the action to if if this
   * wasn't called on the UI thread.
   * @return True if the action was already executed before this function
   * returned, or false if the action was posted to the {@link Handler}.
   */
  @Deprecated
  public static boolean runOnUIThread(Runnable action, Handler handler) {
    return runOnUIThread(handler, action);
  }
  
  /**
   * Runs the given {@link Runnable} immediately if this function is called
   * on the UI thread. Otherwise, it is posted to the given {@link Handler}
   * and executed on its bound thread. Though it is assumed that the given
   * {@link Handler} is bound to the UI thread, it is not necessary, and it
   * will execute the action either way.
   * @param handler The {@link Handler} to post the action to if if this
   * wasn't called on the UI thread.
   * @param action The {@link Runnable} to execute.
   * @return True if the action was already executed before this function
   * returned, or false if the action was posted to the {@link Handler}.
   */
  public static boolean runOnUIThread(Handler handler, Runnable action) {
    if (isOnUIThread()) {
      action.run();
      return true;
    } else {
      if (!handler.post(action)) {
        runOnUIThread(action);
      }
      return false;
    }
  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java