Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a3dc86fa authored by Shai Barack's avatar Shai Barack Committed by Android (Google) Code Review
Browse files

Merge "Note performance issues in some Handler methods and how to avoid them" into main

parents 8aa5330a 45da2014
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
@@ -620,6 +620,11 @@ public class Handler {

    /**
     * Remove any pending posts of Runnable r that are in the message queue.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to cancel a pending
     * operation. For instance, maintain a flag to indicate whether the operation should be
     * performed differently or not at all once it is dispatched.
     */
    public final void removeCallbacks(@NonNull Runnable r) {
        mQueue.removeMessages(this, r, null);
@@ -629,6 +634,11 @@ public class Handler {
     * Remove any pending posts of Runnable <var>r</var> with Object
     * <var>token</var> that are in the message queue.  If <var>token</var> is null,
     * all callbacks will be removed.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to cancel a pending
     * operation. For instance, maintain a flag to indicate whether the operation should be
     * performed differently or not at all once it is dispatched.
     */
    public final void removeCallbacks(@NonNull Runnable r, @Nullable Object token) {
        mQueue.removeMessages(this, r, token);
@@ -807,6 +817,11 @@ public class Handler {
     * the `Runnable` is internally wrapped with a `Message` whose `what` is 0.
     * Calling `removeMessages(0)` will remove all messages without a `what`,
     * including posted `Runnable`s.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to cancel a pending
     * operation. For instance, maintain a flag to indicate whether the operation should be
     * performed differently or not at all once it is dispatched.
     */
    public final void removeMessages(int what) {
        mQueue.removeMessages(this, what, null);
@@ -816,6 +831,11 @@ public class Handler {
     * Remove any pending posts of messages with code 'what' and whose obj is
     * 'object' that are in the message queue.  If <var>object</var> is null,
     * all messages will be removed.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to cancel a pending
     * operation. For instance, maintain a flag to indicate whether the operation should be
     * performed differently or not at all once it is dispatched.
     */
    public final void removeMessages(int what, @Nullable Object object) {
        mQueue.removeMessages(this, what, disallowNullArgumentIfShared(object));
@@ -882,6 +902,11 @@ public class Handler {
    /**
     * Check if there are any pending posts of messages with code 'what' in
     * the message queue.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to track if messages were
     * enqueued and dispatched. For instance, maintain a counter, incrementing it when enqueuing a
     * message and decrementing it when handling a message.
     */
    public final boolean hasMessages(int what) {
        return mQueue.hasMessages(this, what, null);
@@ -889,6 +914,12 @@ public class Handler {

    /**
     * Return whether there are any messages or callbacks currently scheduled on this handler.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to track if messages were
     * enqueued and dispatched. For instance, maintain a counter, incrementing it when enqueuing a
     * message and decrementing it when handling a message.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
@@ -901,6 +932,11 @@ public class Handler {
    /**
     * Check if there are any pending posts of messages with code 'what' and
     * whose obj is 'object' in the message queue.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to track if messages were
     * enqueued and dispatched. For instance, maintain a counter, incrementing it when enqueuing a
     * message and decrementing it when handling a message.
     */
    public final boolean hasMessages(int what, @Nullable Object object) {
        return mQueue.hasMessages(this, what, object);
@@ -910,6 +946,11 @@ public class Handler {
     * Check if there are any pending posts of messages with code 'what' and
     * whose obj is 'object' in the message queue.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to track if messages were
     * enqueued and dispatched. For instance, maintain a counter, incrementing it when enqueuing a
     * message and decrementing it when handling a message.
     *
     *@hide
     */
    public final boolean hasEqualMessages(int what, @Nullable Object object) {
@@ -919,6 +960,11 @@ public class Handler {
    /**
     * Check if there are any pending posts of messages with callback r in
     * the message queue.
     *
     * <p>This operation is worst case O(n) in the number of messages in the queue, and should be
     * avoided. Instead consider another mechanism outside of the queue to track if messages were
     * enqueued and dispatched. For instance, maintain a counter, incrementing it when enqueuing a
     * message and decrementing it when handling a message.
     */
    public final boolean hasCallbacks(@NonNull Runnable r) {
        return mQueue.hasMessages(this, r, null);
+19 −4
Original line number Diff line number Diff line
@@ -22,10 +22,25 @@ import android.annotation.Nullable;
import java.util.concurrent.Executor;

/**
 * A {@link Thread} that has a {@link Looper}.
 * The {@link Looper} can then be used to create {@link Handler}s.
 * <p>
 * Note that just like with a regular {@link Thread}, {@link #start()} must still be called.
 * A {@link Thread} that has a {@link Looper}. The {@link Looper} can then be used to create {@link
 * Handler}s.
 *
 * <p>Note that just like with a regular {@link Thread}, {@link #start()} must still be called.
 *
 * <p>Use this class if you must work with the {@link Handler} API and need a {@link Thread} to do
 * the handling on. Otherwise, consider using a {@link java.util.concurrent.Executor} or {@link
 * java.util.concurrent.ExecutorService} instead. <br>
 * Executors offer more flexibility with regards to threading. Work submitted to an Executor can be
 * set to run on another thread, on one of several threads from a static or dynamic pool, or on the
 * caller's thread, depending on your needs. <br>
 * {link @link java.util.concurrent.Executor} offers a simpler API that is easier to use.
 * {link @link java.util.concurrent.ExecutorService} offers the richer {@link
 * java.util.concurrent.Future} API, which you can use to monitor task status, cancel tasks,
 * propagate exceptions, and chain multiple pending tasks. <br>
 * {link @link java.util.concurrent.Executors} is a convenient factory class that makes it easy to
 * create {@link java.util.concurrent.Executor}s to meet different needs. It creates {@link
 * java.util.concurrent.Executor}s with concurrent work queues that ensure that multiple threads may
 * enqueue and perform work at the same time without encountering lock contention.
 */
@android.ravenwood.annotation.RavenwoodKeepWholeClass
public class HandlerThread extends Thread {