Java/개념
JAVA 쓰레드의 우선순위
정복잉
2020. 7. 14. 02:38
반응형
package day19;
//쓰레드 우선순위
public class ThreadExam05 {
public static void main(String[] args) {
ThreadGroup main = Thread.currentThread().getThreadGroup(); //쓰레드 그룹
//쓰레드를 상속받은 클래스 객체화
Exam01 e1 = new Exam01();
Exam02 e2 = new Exam02();
e2.setPriority(7); //쓰레드 우선순위 설정
//쓰레드 우선순위 반환
System.out.println("Priority od e1(-) : "+e1.getPriority());
System.out.println("Priority od e2(|) : "+e2.getPriority());
System.out.println("Active ThreadGroup : " + main.activeGroupCount()+", Active Thread : "+main.activeCount());
main.list();
e1.start();
e2.start();
}
}
class Exam01 extends Thread{
@Override
public void run() {
for(int i=0;i<300;i++) {
System.out.print("-");
try {
Thread.sleep((int)(Math.random()));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Exam02 extends Thread{
@Override
public void run() {
for(int i=0;i<300;i++) {
System.out.print("|");
try {
Thread.sleep((int)(Math.random()));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
※개념정리
보통 멀티쓰레드를 사용하게 되며 그안에 순위가 존재한다.
main메서드의 우선순위는 5이며, mainn 메서드 내에서 생성되는 쓰레드들도 우선순위가 5이다.
쓰레드 우선순위 지정하기
void setPriority(int newPriority) - 쓰레드의 우선순위를 지정한 값으로 변경
ex) e2.setPriority(7);
int getPriority( ) 쓰레드의 우선순위 반환
스케줄링 - 어떤 원칙과 순서로 쓰레드를 수행시킬 것인지 결정하는 것
자바런타임시스템은 우선순위 스케줄링을 이용