thread_seminar
TRANSCRIPT
Process
Program ProcessRun
single-thread Process = resources + thread multi-thread Process = resources + thread + thread + …
Realization & Run
1. Thread 클래스를 상속 class MyThread extends Thread { public void run() { ~~~~~~ } //Thread클래스의 run()을 오버라이딩
}
2. Runnable 인터페이스를 구현 class MyThread implements Runnable { public void run() { ~~~~~~ } //Runnable인터페이스의 추상메서드 run()을 구현
}
Realization & Run
public class Thread { private Runnable r; //Runnable을 구현한 클래스의 인스턴스를 참조하기 위한 변수 선언
public Thread(Runnable r) { this.r = r;
} public void run() { if (r != null) r.run(); //Runnable인터페이스를 구현한
} 인스턴스의 run()을 호출
Multi-thread
thread start
thread end
thread start
thread start
thread end
thread end
thread create
thread create
Multi-thread
멀티쓰레딩의 장점
- CPU의 사용률을 향상시킨다.
- 자원을 보다 효율적으로 사용할 수 있다.
- 사용자에 대한 응답성이 향상된다.
- 작업이 분리되어 코드가 간결해진다.
Priority
void setPriority(int) : thread의 우선순위를 지정 int getPriority() : thread의 우선순위를 반환
public static final int MAX_PRIORITY = 10 : 최대우선순위 public static final int MIN_PRIORITY = 1 : 최소우선순위 public static final int NORM_PRIORITY = 5 : 보통우선순위