스레드 우선순위,그룹

개발하는개발자·2022년 9월 8일
0

Java Thread

목록 보기
5/10

우선순위

스레드는 우선순위를 통해 각 스레드별 작업시간을 다르게 가지게 할 수 있다.

void setPriority(int newPriority) : 스레드의 우선순위를 지정한 값으로 변경한다.
void getPriority() : 스레드의 우선순위를 반환한다.

스레드가 가질 수 있는 우선순위 범위는 1~10이고 숫자가 높을수록 우선순위가 높다. 스레드의 우선순위는 스레드를 생성한 스레드로부터 상속받는다. main메서드를 수행하는 스레드의 우선순위는 5다. 스레드의 우선순위 변경은 스레드를 실행하기전에만 가능하다.

//스레드 우선순위 지정
class ThreadEx8 {
    public static void main(String args[]) {
        ThreadEx8_1 th1 = new ThreadEx8_1();
        ThreadEx8_2 th2 = new ThreadEx8_2();

        th2.setPriority(7);

        System.out.println("Priority of th1(-) : " + th1.getPriority() );
        System.out.println("Priority of th2(|) : " + th2.getPriority() );
        th1.start();
        th2.start();
    }
}

class ThreadEx8_1 extends Thread {
    public void run() {
        for(int i=0; i < 300; i++) {
            System.out.print("-");
            for(int x=0; x < 10000000; x++);
        }
    }
}

class ThreadEx8_2 extends Thread {
    public void run() {
        for(int i=0; i < 300; i++) {
            System.out.print("|");
            for(int x=0; x < 10000000; x++);
        }
    }
}

스레드 그룹

서로 관련된 스레드를 그룹으로 다룬다. 자바 최상위 스레드 그룹은 main 스레드 그룹이며 스레드 그룹은 하위 스레드 그룹과 스레드를 멤버로 가진다.

가비지컬렉션은 위의 System영역에 속한다.

profile
하루에 하나씩 배우자

0개의 댓글