Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Inherited Constants](https://developer.android.com/reference/android/os/HandlerThread#inhconstants) \| [Ctors](https://developer.android.com/reference/android/os/HandlerThread#pubctors) \| [Methods](https://developer.android.com/reference/android/os/HandlerThread#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/os/HandlerThread#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/os/HandlerThread#inhmethods)

# HandlerThread

*** ** * ** ***


`
public


class
HandlerThread
`


`


`

`

extends https://developer.android.com/reference/java/lang/Thread


`

`


`

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) |||
| ↳ | [java.lang.Thread](https://developer.android.com/reference/java/lang/Thread) ||
|   | ↳ | android.os.HandlerThread |

<br />

*** ** * ** ***

A `https://developer.android.com/reference/java/lang/Thread` that has a `https://developer.android.com/reference/android/os/Looper`. The `https://developer.android.com/reference/android/os/Looper` can then be used to create `https://developer.android.com/reference/android/os/Handler`s. Just like with a regular `https://developer.android.com/reference/java/lang/Thread`, `https://developer.android.com/reference/java/lang/Thread#start()` must still be called.

*Use this class only if you must work with the `https://developer.android.com/reference/android/os/Handler` API and need a `https://developer.android.com/reference/java/lang/Thread` to do the handling on that is not an existing `https://developer.android.com/reference/android/os/Looper` thread, such as `https://developer.android.com/reference/android/os/Looper#getMainLooper()`.* Otherwise, prefer `https://developer.android.com/reference/java/util/concurrent/Executor` or `https://developer.android.com/reference/java/util/concurrent/ExecutorService`, or Kotlin [coroutines](https://developer.android.com/topic/libraries/architecture/coroutines).

Note that many APIs that required a `https://developer.android.com/reference/android/os/Handler` in older SDK versions offer a newer
alternative that accepts an `https://developer.android.com/reference/java/util/concurrent/Executor` instead. Always prefer to use
the newer API if available.

## Alternatives to `HandlerThread`

`https://developer.android.com/reference/java/util/concurrent/Executor`s offer more flexibility with regards to threading. Work
submitted to an `https://developer.android.com/reference/java/util/concurrent/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.

{link @link java.util.concurrent.Executor} offers a simpler API that is easier to use compared
to `https://developer.android.com/reference/android/os/Handler`. {link @link java.util.concurrent.ExecutorService} offers the richer `https://developer.android.com/reference/java/util/concurrent/Future` API, which you can use to monitor task status, cancel tasks,
propagate exceptions, and chain multiple pending tasks.

{link @link java.util.concurrent.Executors} is a factory for various `https://developer.android.com/reference/java/util/concurrent/Executor`s that meet common concurrency needs. These `https://developer.android.com/reference/java/util/concurrent/Executor`s use work queues that offer better concurrency and reduced
contention than `HandlerThread`.

On Kotlin, [coroutines](https://developer.android.com/topic/libraries/architecture/coroutines) may
be used to handle concurrency.

## Common `HandlerThread` performance issues

Apps that use `HandlerThread` may encounter the following performance issues:

- **Excessive thread creation** : A `HandlerThread` is a `https://developer.android.com/reference/java/lang/Thread`. Every system thread costs some resident memory, whether it is working or if it's idle. If your app has a large number of `HandlerThread`s each dedicated to a single type of task, rather than for instance a `https://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor` that can grow and shrink in size according to demand, then the additional idle `HandlerThread`s will be wasting memory.
- **Lock contention** : `HandlerThread` uses a `https://developer.android.com/reference/android/os/Looper` which in turn uses a `https://developer.android.com/reference/android/os/MessageQueue`. `https://developer.android.com/reference/android/os/MessageQueue` uses a single lock to synchronize access to its underlying queue. Any threads attempting to enqueue messages at the same time, and the `HandlerThread` itself when attempting to dequeue the next message to handle, will block each other.
- **Priority inversion** : A high-priority `HandlerThread` can become blocked by a lower-priority thread, for instance if the former is trying to enqueue a message while the latter is trying to dequeue the next message to handle, or vice versa.

The best way to avoid these issues is to use `https://developer.android.com/reference/java/util/concurrent/Executor`s or [Kotlin
coroutines](https://developer.android.com/topic/libraries/architecture/coroutines) instead of `HandlerThread`.

<br />

## Summary

### Inherited constants

From class `https://developer.android.com/reference/java/lang/Thread` `int` `https://developer.android.com/reference/java/lang/Thread#MAX_PRIORITY`

The maximum priority that a thread can have.


`int` `https://developer.android.com/reference/java/lang/Thread#MIN_PRIORITY`

The minimum priority that a thread can have.


`int` `https://developer.android.com/reference/java/lang/Thread#NORM_PRIORITY`

The default priority that is assigned to a thread.


| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/os/HandlerThread#HandlerThread(java.lang.String)(https://developer.android.com/reference/java/lang/String name) ` |
| ` https://developer.android.com/reference/android/os/HandlerThread#HandlerThread(java.lang.String,%20int)(https://developer.android.com/reference/java/lang/String name, int linuxPriority) ` Constructs a HandlerThread. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/os/Looper` | ` https://developer.android.com/reference/android/os/HandlerThread#getLooper()() ` This method returns the Looper associated with this thread. |
| ` int` | ` https://developer.android.com/reference/android/os/HandlerThread#getThreadId()() ` Returns the identifier of this thread. |
| ` boolean` | ` https://developer.android.com/reference/android/os/HandlerThread#quit()() ` Quits the handler thread's looper. |
| ` boolean` | ` https://developer.android.com/reference/android/os/HandlerThread#quitSafely()() ` Quits the handler thread's looper safely. |
| ` void` | ` https://developer.android.com/reference/android/os/HandlerThread#run()() ` If this thread was constructed using a separate `Runnable` run object, then that `Runnable` object's `run` method is called; otherwise, this method does nothing and returns. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/os/HandlerThread#onLooperPrepared()() ` Call back method that can be explicitly overridden if needed to execute some setup before Looper loops. |

### Inherited methods

From class `

https://developer.android.com/reference/java/lang/Thread

` `


static


int` `
https://developer.android.com/reference/java/lang/Thread#activeCount()()
`

Returns an estimate of the number of active threads in the current
thread's [thread group](https://developer.android.com/reference/java/lang/ThreadGroup) and its
subgroups.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#checkAccess()()
`

*This method was deprecated
in API level 36.
This method is only useful in conjunction with
[the Security Manager](https://developer.android.com/reference/java/lang/SecurityManager), which is
deprecated and subject to removal in a future release.
Consequently, this method is also deprecated and subject to
removal. There is no replacement for the Security Manager or this
method.*


`


https://developer.android.com/reference/java/lang/Object` `
https://developer.android.com/reference/java/lang/Thread#clone()()
`

Throws CloneNotSupportedException as a Thread can not be meaningfully
cloned.


`


static


https://developer.android.com/reference/java/lang/Thread` `
https://developer.android.com/reference/java/lang/Thread#currentThread()()
`

Returns a reference to the currently executing thread object.


`


static


void` `
https://developer.android.com/reference/java/lang/Thread#dumpStack()()
`

Prints a stack trace of the current thread to the standard error stream.


`


static


int` `
https://developer.android.com/reference/java/lang/Thread#enumerate(java.lang.Thread[])(https://developer.android.com/reference/java/lang/Thread tarray)
`

Copies into the specified array every active thread in the current
thread's thread group and its subgroups.


`


static


https://developer.android.com/reference/java/util/Map<https://developer.android.com/reference/java/lang/Thread, https://developer.android.com/reference/java/lang/StackTraceElement>` `
https://developer.android.com/reference/java/lang/Thread#getAllStackTraces()()
`

Returns a map of stack traces for all live threads.


`


https://developer.android.com/reference/java/lang/ClassLoader` `
https://developer.android.com/reference/java/lang/Thread#getContextClassLoader()()
`

Returns the context `ClassLoader` for this thread.


`


static


https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler` `
https://developer.android.com/reference/java/lang/Thread#getDefaultUncaughtExceptionHandler()()
`

Returns the default handler invoked when a thread abruptly terminates
due to an uncaught exception.


`


long` `
https://developer.android.com/reference/java/lang/Thread#getId()()
`

*This method was deprecated
in API level 36.
This method is not final and may be overridden to return a
value that is not the thread ID. Use `https://developer.android.com/reference/java/lang/Thread#threadId()` instead.*


`


final

https://developer.android.com/reference/java/lang/String` `
https://developer.android.com/reference/java/lang/Thread#getName()()
`

Returns this thread's name.


`


final

int` `
https://developer.android.com/reference/java/lang/Thread#getPriority()()
`

Returns this thread's priority.


`


https://developer.android.com/reference/java/lang/StackTraceElement` `
https://developer.android.com/reference/java/lang/Thread#getStackTrace()()
`

Returns an array of stack trace elements representing the stack dump
of this thread.


`


https://developer.android.com/reference/java/lang/Thread.State` `
https://developer.android.com/reference/java/lang/Thread#getState()()
`

Returns the state of this thread.


`


final

https://developer.android.com/reference/java/lang/ThreadGroup` `
https://developer.android.com/reference/java/lang/Thread#getThreadGroup()()
`

Returns the thread group to which this thread belongs.


`


https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler` `
https://developer.android.com/reference/java/lang/Thread#getUncaughtExceptionHandler()()
`

Returns the handler invoked when this thread abruptly terminates
due to an uncaught exception.


`


static


boolean` `
https://developer.android.com/reference/java/lang/Thread#holdsLock(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj)
`

Returns `true` if and only if the current thread holds the
monitor lock on the specified object.


`


void` `
https://developer.android.com/reference/java/lang/Thread#interrupt()()
`

Interrupts this thread.


`


static


boolean` `
https://developer.android.com/reference/java/lang/Thread#interrupted()()
`

Tests whether the current thread has been interrupted.


`


final

boolean` `
https://developer.android.com/reference/java/lang/Thread#isAlive()()
`

Tests if this thread is alive.


`


final

boolean` `
https://developer.android.com/reference/java/lang/Thread#isDaemon()()
`

Tests if this thread is a daemon thread.


`


boolean` `
https://developer.android.com/reference/java/lang/Thread#isInterrupted()()
`

Tests whether this thread has been interrupted.


`


final

boolean` `
https://developer.android.com/reference/java/lang/Thread#isVirtual()()
`

Returns `true` if this thread is a virtual thread.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#join()()
`

Waits for this thread to die.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#join(long)(long millis)
`

Waits at most `millis` milliseconds for this thread to
die.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#join(long,%20int)(long millis, int nanos)
`

Waits at most `millis` milliseconds plus
`nanos` nanoseconds for this thread to die.


`


final

boolean` `
https://developer.android.com/reference/java/lang/Thread#join(java.time.Duration)(https://developer.android.com/reference/java/time/Duration duration)
`

Waits for this thread to terminate for up to the given waiting duration.


`


static


void` `
https://developer.android.com/reference/java/lang/Thread#onSpinWait()()
`

Indicates that the caller is momentarily unable to progress, until the
occurrence of one or more actions on the part of other activities.


`


void` `
https://developer.android.com/reference/java/lang/Thread#run()()
`

If this thread was constructed using a separate
`Runnable` run object, then that
`Runnable` object's `run` method is called;
otherwise, this method does nothing and returns.


`


void` `
https://developer.android.com/reference/java/lang/Thread#setContextClassLoader(java.lang.ClassLoader)(https://developer.android.com/reference/java/lang/ClassLoader cl)
`

Sets the context ClassLoader for this Thread.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#setDaemon(boolean)(boolean on)
`

Marks this thread as either a [daemon](https://developer.android.com/reference/java/lang/Thread#isDaemon()) thread
or a user thread.


`


static


void` `
https://developer.android.com/reference/java/lang/Thread#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)(https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler eh)
`

Set the default handler invoked when a thread abruptly terminates
due to an uncaught exception, and no other handler has been defined
for that thread.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#setName(java.lang.String)(https://developer.android.com/reference/java/lang/String name)
`

Changes the name of this thread to be equal to the argument `name`.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#setPriority(int)(int newPriority)
`

Changes the priority of this thread.


`


void` `
https://developer.android.com/reference/java/lang/Thread#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)(https://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler eh)
`

Set the handler invoked when this thread abruptly terminates
due to an uncaught exception.


`


static


void` `
https://developer.android.com/reference/java/lang/Thread#sleep(long,%20int)(long millis, int nanos)
`

Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds plus the specified
number of nanoseconds, subject to the precision and accuracy of system
timers and schedulers.


`


static


void` `
https://developer.android.com/reference/java/lang/Thread#sleep(long)(long millis)
`

Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds, subject to
the precision and accuracy of system timers and schedulers.


`


static


void` `
https://developer.android.com/reference/java/lang/Thread#sleep(java.time.Duration)(https://developer.android.com/reference/java/time/Duration duration)
`

Causes the currently executing thread to sleep (temporarily cease
execution) for the specified duration, subject to the precision and
accuracy of system timers and schedulers.


`


void` `
https://developer.android.com/reference/java/lang/Thread#start()()
`

Causes this thread to begin execution; the Java Virtual Machine
calls the `run` method of this thread.


`


final

void` `
https://developer.android.com/reference/java/lang/Thread#stop()()
`

*This method was deprecated
in API level 15.
This method was originally designed to force a thread to stop
and throw a `ThreadDeath` as an exception. It was inherently unsafe.
Stopping a thread with
Thread.stop causes it to unlock all of the monitors that it
has locked (as a natural consequence of the unchecked
`ThreadDeath` exception propagating up the stack). If
any of the objects previously protected by these monitors were in
an inconsistent state, the damaged objects become visible to
other threads, potentially resulting in arbitrary behavior. Many
uses of `stop` should be replaced by code that simply
modifies some variable to indicate that the target thread should
stop running. The target thread should check this variable
regularly, and return from its run method in an orderly fashion
if the variable indicates that it is to stop running. If the
target thread waits for long periods (on a condition variable,
for example), the `interrupt` method should be used to
interrupt the wait.
For more information, see
[Why
are Thread.stop, Thread.suspend and Thread.resume Deprecated?](https://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html).*


`


final

long` `
https://developer.android.com/reference/java/lang/Thread#threadId()()
`

Returns the identifier of this Thread.


`


https://developer.android.com/reference/java/lang/String` `
https://developer.android.com/reference/java/lang/Thread#toString()()
`

Returns a string representation of this thread, including the
thread's name, priority, and thread group.


`


static


void` `
https://developer.android.com/reference/java/lang/Thread#yield()()
`

A hint to the scheduler that the current thread is willing to yield
its current use of a processor.


From class `

https://developer.android.com/reference/java/lang/Object

` `


https://developer.android.com/reference/java/lang/Object` `
https://developer.android.com/reference/java/lang/Object#clone()()
`

Creates and returns a copy of this object.


`


boolean` `
https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj)
`

Indicates whether some other object is "equal to" this one.


`


void` `
https://developer.android.com/reference/java/lang/Object#finalize()()
`

Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.


`


final

https://developer.android.com/reference/java/lang/Class<?>` `
https://developer.android.com/reference/java/lang/Object#getClass()()
`

Returns the runtime class of this `Object`.


`


int` `
https://developer.android.com/reference/java/lang/Object#hashCode()()
`

Returns a hash code value for the object.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#notify()()
`

Wakes up a single thread that is waiting on this object's
monitor.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#notifyAll()()
`

Wakes up all threads that are waiting on this object's monitor.


`


https://developer.android.com/reference/java/lang/String` `
https://developer.android.com/reference/java/lang/Object#toString()()
`

Returns a string representation of the object.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis)
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*, or until a
certain amount of real time has elapsed.


`


final

void` `
https://developer.android.com/reference/java/lang/Object#wait()()
`

Causes the current thread to wait until it is awakened, typically
by being *notified* or *interrupted*.


From interface `

https://developer.android.com/reference/java/lang/Runnable

` `
abstract


void` `
https://developer.android.com/reference/java/lang/Runnable#run()()
`

When an object implementing interface `Runnable` is used
to create a thread, starting the thread causes the object's
`run` method to be called in that separately executing
thread.


## Public constructors

### HandlerThread

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public HandlerThread (String name)
```

<br />

<br />

| Parameters ||
|---|---|
| `name` | `String` <br /> |

### HandlerThread

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public HandlerThread (String name, 
                int linuxPriority)
```

Constructs a HandlerThread.

<br />

| Parameters ||
|---|---|
| `name` | `String`: The name of the thread. <br /> |
| `linuxPriority` | `int`: A Linux priority (niceness) value from -20 to 19, such as the `THREAD_PRIORITY_*` constants in `https://developer.android.com/reference/android/os/Process`. Do not pass `https://developer.android.com/reference/java/lang/Thread` priorities (such as the inherited `https://developer.android.com/reference/java/lang/Thread#MIN_PRIORITY`), which use an incompatible scale. <br /> |

## Public methods

### getLooper

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Looper getLooper ()
```

This method returns the Looper associated with this thread. If this thread not been started
or for any reason isAlive() returns false, this method will return null. If this thread
has been started, this method will block until the looper has been initialized.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/Looper` | The looper. <br /> |

### getThreadId

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getThreadId ()
```

Returns the identifier of this thread. See Process.myTid().

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### quit

Added in [API level 5](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean quit ()
```

Quits the handler thread's looper.


Causes the handler thread's looper to terminate without processing any
more messages in the message queue.


Any attempt to post messages to the queue after the looper is asked to quit will fail.
For example, the `https://developer.android.com/reference/android/os/Handler#sendMessage(android.os.Message)` method will return false.


If `https://developer.android.com/reference/android/os/HandlerThread#quit()` or `https://developer.android.com/reference/android/os/HandlerThread#quitSafely()` is called multiple times, the first call
will have an effect and the subsequent calls will be no-ops.


Using this method may be unsafe because some messages may not be delivered
before the looper terminates. Consider using `https://developer.android.com/reference/android/os/HandlerThread#quitSafely()` instead to ensure
that all pending work is completed in an orderly manner.

<br />

<br />

| Returns ||
|---|---|
| `boolean` | True if the looper looper has been asked to quit or false if the thread had not yet started running. <br /> |

**See also:**

- `https://developer.android.com/reference/android/os/HandlerThread#quitSafely()`

### quitSafely

Added in [API level 18](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean quitSafely ()
```

Quits the handler thread's looper safely.


Causes the handler thread's looper to terminate as soon as all remaining messages
in the message queue that are already due to be delivered have been handled.
Pending delayed messages with due times in the future will not be delivered.


Any attempt to post messages to the queue after the looper is asked to quit will fail.
For example, the `https://developer.android.com/reference/android/os/Handler#sendMessage(android.os.Message)` method will return false.


If the thread has not been started or has finished (that is if
`https://developer.android.com/reference/android/os/HandlerThread#getLooper()` returns null), then false is returned.
Otherwise the looper is asked to quit and true is returned.


If `https://developer.android.com/reference/android/os/HandlerThread#quit()` or `https://developer.android.com/reference/android/os/HandlerThread#quitSafely()` is called multiple times, the first call
will have an effect and the subsequent calls will be no-ops.

<br />

<br />

| Returns ||
|---|---|
| `boolean` | True if the looper looper has been asked to quit or false if the thread had not yet started running. <br /> |

### run

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void run ()
```

If this thread was constructed using a separate
`Runnable` run object, then that
`Runnable` object's `run` method is called;
otherwise, this method does nothing and returns.


Subclasses of `Thread` should override this method.

<br />

## Protected methods

### onLooperPrepared

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected void onLooperPrepared ()
```

Call back method that can be explicitly overridden if needed to execute some
setup before Looper loops.

<br />