Post the Runnable instance with the following code to the Handler provided: public void run() { Looper.myLooper().quit(); } - Android java.lang

Android examples for java.lang:Thread

Description

Post the Runnable instance with the following code to the Handler provided: public void run() { Looper.myLooper().quit(); }

Demo Code


import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import java.util.concurrent.atomic.AtomicInteger;

public class Main{
    private static Runnable sStopper;
    /**/*from  w  w w  . ja v  a  2  s .  c  o  m*/
     * @see #stopThread(Handler, boolean)
     */
    public static void stopThread(Handler handler) {
        stopThread(handler, true);
    }
    /**
     * Post the {@link Runnable} instance with the following code to the {@link Handler} provided:
     * <pre>
     * public void run() {
     *     Looper.myLooper().quit();
     * }
     * </pre>
     *
     * @param handler target handler, can not be null
     * @param asap    if true then method {@link Handler#postAtFrontOfQueue(Runnable)} will be used.
     */
    public static void stopThread(Handler handler, boolean asap) {
        Runnable stopper = sStopper;
        if (stopper == null) {
            stopper = new ThreadUtils();
            sStopper = stopper;
        }

        if (asap) {
            handler.postAtFrontOfQueue(stopper);
        } else {
            handler.post(stopper);
        }
    }
}

Related Tutorials