본문 바로가기
개발/JAVA

[4주차] 직접 해보는 손코딩! Thread

by Remover 2020. 2. 29.
반응형
public class App {
    
    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable(){
        
            public void save(){
                System.out.println("작업 내용을 저장함.");
            }

            @Override
            public void run() {
                while(true){
                    try{
                        Thread.sleep(1000);
                    }catch(Exception e){

                    }
                    save();
                }
                
            }
        };

        Thread task = new Thread(r);

        task.setDaemon(true);
        task.start();

        try{
            Thread.sleep(4000);
        }catch(Exception e){

        }
        System.out.println("메인 쓰레드 종료!");
    }
}

550쪽 직접 해보는 손코딩을 진행해보았습니다.

책에서는 Thread를 상속 받는 별도의 Class를 작성해서 사용하였지만, 저 같은 경우 Runnable 함수를 사용해서

작성을 진행하였습니다.

 

결과 : 

댓글