Thread
참고 URL : http://javaexpert.springnote.com/pages/2103306
Thread Life cycle
1. New : thread 객체 생성. thread의 생명 주기 시작
2. Runnable : start() 함수 호출. Runnable 상태로 이동.
3. Running : Runnable 상태의 thread가 스케줄러에 의해 동작 상태로 이동. run() 함수가 수행되는 상태.
4. Blocked : 입출력 요청, 동기화 블럭 수행시 Blocked 상태로 이동.
5. Dead : thread 종료. 재시작 불가.
2 Ways of Thread Creation
1. Thread Class를 이용한 thread 생성
|
- package Thread;
- public class CreateThread extends Thread {
public void run() { }
- /**
* Thread 인스턴스를 생성(New)하여 start() 메서드를 호출(Runnable)한다. * 새로운 스레드가 생성되고 스케줄러에 의해 Runnable에서 Running 상태로 이동되면서 * run() 메서드가 호출(Running)된다. * 이때 start() 메서드 대신 run() 메서드를 바로 호출하면 기존 메인 스레드가 계속 * 동작하게 된다. */ public static void main(String[] args) { CreateThread ct = new CreateThread(); ct.start(); }
- }
|
2. Runnable을 이용한 thread 생성
|
- package Thread;
- public class CreateRunnable implements Runnable {
public void run() { } /** * Runnable 객체를 생성하여 Thread(Runnable t)를 사용하여 스레스를 생성. */ public static void main(String[] args) { CreateRunnable ct = new CreateRunnable(); Thread t = new Thread(ct); t.start(); }
- }
|