본문 바로가기

Java SE/Stopping Thread

Stopping a Thread

Thread를 멈추기 위한 방법

// Create and start the thread
    MyThread thread = new MyThread();
    thread.start();
   
    // Do work...
   
    // Stop the thread
    thread.allDone = true;
   
    class MyThread extends Thread {
        boolean allDone = false;
   
        // This method is called when the thread runs
        public void run() {
            while (true) {
                // Do work...
   
                if (allDone) {
                    return;
                }
   
                // Do work...
            }
        }
    }