쓰레드와 sleep 메서드

쓰레드와 sleep 메서드

  • 50밀리세컨드마다 쓰레드 이름을 출력하게 만들어 보자.
  • 쓰레드와 sleep 메서드의 사용법을 배운다.
  • Thread.sleep 메서드를 사용하고 예외처리를 해준다.
?
public class  SleepThread extends Thread{
    public SleepThread(String name){
        setName(name);
    }
    public void run(){show();}
    public void show(){
        for(int i=0 ;i<50;i++){
            print();
            try{
                Thread.sleep(50);//50/1000 초
            }catch(InterruptedException ite){}
        }
    }
    public void print(){
            System.out.print(getName());//Thread에서
    }
}
?
public class  SleepThreadMain{
    public static void main(String[] args) {
        SleepThread t1=new SleepThread("a");
        SleepThread t2=new SleepThread("b");
        SleepThread t3=new SleepThread("c");
 
        t2.setPriority(7);//1~10 클수록 우서순의
        t1.start();//t2가 t1보다 우선이지만
        try{
            t1.join();//t1을 끝낸후 t2, t3를 실행한다.
        }catch(InterruptedException ite){}
        t2.start();
        t3.start();
    }
}

댓글

이 블로그의 인기 게시물

파일처리(한번에 모두읽기, 라인단위로 읽기, 쓰기, 리스트처리, 특정길이만큼 읽기)

AWS 가용성,확장성

math 함수 쓰기