스레드 우선순위에 대해 질문 드립니다.
초코맛사탕
의도 하고자 한바는..
스레드 세개를 생성해서 우선순위를 정해주고 스레드 실행시 메세지를 출력하며
스레드가 실행될떄마다 count 를 증가해 맨 마지막에 총 스레드 가 샐행된 수를
출력하는건데요
이상하게 실행이 잘 안되네요.....어이 없는 결과가 나와버리질 안나....
스레드 가 실행되는 도중 뚝뚝 끊기기도 하고...이건 아마 while 문 떄매 그런거 같은데
제 머리상 도데체가 좋은 구조가 생각이 안나네요,.,,ㅜㅜ
이거 실행이 잘 되도록 어디부분을 좀더 손 봐야 할까요?
class SimpleThread extends Thread{
static int max,norm,min;
public SimpleThread(String s){super(s);
max=norm=min=0;
}
public void run(){
for(int i=0;i15;i++){
if(getName()==Max Priority thread)
max++;
if(getName() ==Normal Priority thread);
norm++;
if(getName() ==Min priority thread);
min++;
System.out.println(getName() + \t우선순위 : + getPriority());
try{sleep((int)(Math.random() * 4));
}catch(Exception e){}
}
}
}
class Priority{
static boolean check=false;
public Priority(){}
public static void main(String[] args)
{SimpleThread t1 = new SimpleThread(Max Priority thread);
SimpleThread t2 = new SimpleThread(Normal Priority thread);
SimpleThread t3 = new SimpleThread(Min priority thread);
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
t3.start();
while(true){
if((!t1.isAlive())&&(!t2.isAlive())&&(!t3.isAlive()) )
check=true;
if(check){
System.out.println(max : + SimpleThread.max);
System.out.println(norm :+SimpleThread.norm);
System.out.println(min :+SimpleThread.min);
break;
}
}
}
}
-
큰힘
묘한 코드군요. 의도하신 것과는 좀 안맞는것 같습니다. (모두 15가 나오죠.. 15번 돌렸으니..) 그나마 돌아가게 만드시려면..
1. getName()==\~~~\ 을 getName.equals(\~~~\)
2. while (true) { if((!t1.isAlive())&&(!t2.isAlive())&&(!t3.isAlive()) ) ~~ } 이건 무한 루핑이 걸려서 cpu 100 됩니다. wait, notify 로 고치는게 보통이고 간단하게는