Java/Java
(JAVA) 쓰레드 우선순위
주누
2020. 1. 21. 00:44

쓰레드는 우선순위라는 속성을 가지고 있다.

최소 우선순위
중간 우선순위
최대 우선순위
- 해당 우선순위의 값에 따라 쓰레드가 얻는 실행시간이 달라진다.
- 쓰레드가 수행하는 작업의 중요도에 따라 우선순위를 다르게 설정하여 중요한 작업이 더 많은 작업시간을 가질 수 있게 할 수 있다.
priorityThread_3의 우선순위를 1로 지정
public class PriorityThread {
public static void main(String[] args) {
PriorityThread_2 priorityThread_2 = new PriorityThread_2();
PriorityThread_3 priorityThread_3 = new PriorityThread_3();
priorityThread_3.setPriority(1);
System.out.println("Priority of PriorityThread_2(ㅡ) : "+priorityThread_2.getPriority());
System.out.println("Priority of PriorityThread_3(|) : "+priorityThread_3.getPriority());
priorityThread_2.start();
priorityThread_3.start();
}
}
class PriorityThread_2 extends Thread{
@Override
public void run() {
for (int i=0;i<300;i++) {
System.out.print("ㅡ");
for (int x=0;x<1000000;x++);
}
}
}

실행 결과 main의 우선순위는 기본적으로 5로 되어있는 것을 알 수 있다.
하지만 실행결과로 보아서는 내가 볼 땐... 우선순위에 상관없이 거의 동일하게 작업이 할당된 것 같다...!
나의 운영체제 MacOs에 종속적이기 때문에 (스케쥴러에 종속) 해당 결과가 나온다.
나는 우선순위 쓰레드는 딱 이 정도만 알고 넘어가는 것으로 결정하였다.
코드 참조
https://github.com/mike6321/PURE_JAVA/tree/master/Thread
mike6321/PURE_JAVA
Contribute to mike6321/PURE_JAVA development by creating an account on GitHub.
github.com