Determine if we are on the UI Thread or not. If we are NOT on the UI Thread, we will execute the action in the current thread. - Android java.lang

Android examples for java.lang:Thread

Description

Determine if we are on the UI Thread or not. If we are NOT on the UI Thread, we will execute the action in the current thread.

Demo Code


//package com.java2s;

import android.os.Looper;

public class Main {
    /**// w w  w  .ja v  a2s.c  o m
     * same as calling #runOffUiThread(action,false)
     */
    public static void runOffUiThread(final Runnable action) {
        runOffUiThread(action, false);
    }

    /**
     * Determine if we are on the UI Thread or not.  If we are NOT on the UI Thread, we will execute the action in the current thread.  If
     * we are on the UI Thread, we will spawn a new thread to execute ACTION
     *
     * @param action procedure to run off the UI Thread
     * @param wait   true if we should wait for action to complete before returning to calling process
     */
    public static void runOffUiThread(final Runnable action,
            final boolean wait) {
        if (isOnUiThread()) {
            runOnNewThread(action, wait);
        } else {
            action.run();
        }
    }

    /**
     * Check to see whether current thread is also the UI Thread
     *
     * @return TRUE when current thread is the UI Thread
     */
    public static boolean isOnUiThread() {
        return Looper.getMainLooper().getThread() == Thread.currentThread();
    }

    /**
     * Same as calling #runOnNewThread( action, false )
     */
    public static void runOnNewThread(final Runnable action) {
        runOnNewThread(action, false);
    }

    /**
     * Always spawn a new, non-UI thread process to execute ACTION
     *
     * @param action procedure to run on the new Thread
     * @param wait   true if we should wait for action to complete before returning to calling process
     */
    public static void runOnNewThread(final Runnable action,
            final boolean wait) {
        final Runnable threadRunnable = new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    action.run();
                    if (wait) {
                        this.notify();
                    }
                }
            }
        };

        synchronized (threadRunnable) {
            new Thread(threadRunnable).start();
            waitForAction(threadRunnable, wait);
        }
    }

    protected static void waitForAction(final Runnable action, boolean wait) {
        if (wait) {
            synchronized (action) {
                try {
                    action.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Related Tutorials