JAVA Basic 강의자료] Priority 예제

 

JAVA Basic 강의자료] Priority 예제

 

 

 

 

실무개발자를위한 실무교육 전문교육센터학원
www.oraclejava.co.kr에 오시면 보다 다양한 강좌를 보실 수 있습니다.

 

 

Priority 예제

 

 

*스레드의 기본 우선권은 5로 부여됨


*프로그래머가 임의로 부여 가능
-setPriority(int newPriority)로 설정

 

 

<소스코드>

 

//PriorityThreadMain.java
class SoloThread extends Thread {//스레드상속
  private int number = 0;
  public SoloThread(int n){
    System.out.print(this.getName() + ":스레드시작\t");
    number = n;
  }
  public void run(){
    int i = 0;
    while(i < number){
      System.out.print(getName() + ":p:"+this.getPriority() + ":"+i + "\t");
      try{
        this.sleep(1000);
      }catch(Exception e){System.out.println(e);}
      i++;
    }
    System.out.println("\n" + this.getName() + ":p:“ +  this.getPriority() + ":스레드종료\t");
  }
}

 

 

===============================

 


public class PriorityThreadMain{

  public static void main(String[] args){
    System.out.println("Main메서드시작");
    System.out.println("Thread.MAX_PRIORITY:" +Thread.MAX_PRIORITY);
    System.out.println("Thread.MIN_PRIORITY:" +Thread.MIN_PRIORITY);
    System.out.println("Thread.NORM_PRIORITY:" +Thread.NORM_PRIORITY);

    for(int i=Thread.MIN_PRIORITY; i<Thread.MAX_PRIORITY + 1;i++)
    {
      SoloThread s = new SoloThread(5);
      s.setPriority(i);
      s.start();  
    }
    System.out.println("Main메서드종료");
  }
}

 

+ Recent posts