JAVA 다 중 스 레 드 디자인 모델 10 2 상 패턴

public class CountupThread extends Thread {
    //      
    private long counter = 0;

    //           true
    private volatile boolean shutdownRequested = false;

    //      
    public void shutdownRequest() {
        shutdownRequested = true;
        interrupt();
    }

    //             
    public boolean isShutdownRequested() {
        return shutdownRequested;
    }

    //  
    public final void run() {
        try {
            while (!shutdownRequested) {
                doWork();
            }
        } catch (InterruptedException e) {
        } finally {
            doShutdown();
        }
    }

    //    
    private void doWork() throws InterruptedException {
        counter++;
        System.out.println("doWork: counter = " + counter);
        Thread.sleep(500);
    }

    //     
    private void doShutdown() {
        System.out.println("doShutdown: counter = " + counter);
    }
}

 
 
 
public class Main {
    public static void main(String[] args) {
        System.out.println("main: BEGIN");
        try {
           
            CountupThread t = new CountupThread();
            t.start();

            Thread.sleep(10000);

            System.out.println("main: shutdownRequest");
            t.shutdownRequest();

            System.out.println("main: join");


            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main: END");
    }
}

 

좋은 웹페이지 즐겨찾기