본문 바로가기

Java SE/setDaemon()

setDaemon()


    // This class extends Thread
    class MyThread extends Thread {
        MyThread() {
            // Thread can be set as a daemon thread in the constructor
            setDaemon(true);
        }
    
        // This method is called when the thread runs
        public void run() {
            // Determine if this thread is a daemon thread
            boolean isDaemon = isDaemon();
        }
    }
    // Create the thread
    Thread thread = new MyThread();
    
    // Thread can be set as daemon by the creator
    thread.setDaemon(true);
    
    // Start the thread
    thread.start();