개발하는 두더지

[Android/안드로이드/자바] Thread, Runnable, Handler 에 대해 알아보기 본문

Java,Android

[Android/안드로이드/자바] Thread, Runnable, Handler 에 대해 알아보기

덜지 2016. 7. 22. 01:44

1. Thread 개념 잡기

2. Runnable 개념 잡기

3. Handler 개념 잡기

4. 각각의 사용 방법

5. 예제



Thread

Thread is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Each application has at least one thread running when it is started, the main thread, in the main ThreadGroup. The runtime keeps its own threads in the system thread group.

There are two ways to execute code in a new thread. You can either subclass Thread and overriding its run() method, or construct a new Thread and pass aRunnable to the constructor. In either case, the start() method must be called to actually execute the new Thread.

 

Runnable

Represents a command that can be executed. Often used to run code in a different Thread


Handler

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.



// 스레드에서 UI 변경 작업을 하고 싶다면 핸들러를 쓰면 된다.



Comments