Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Daemon Threads in Java

Tech 3

The setDaemon(boolean on) method marks a thread as a daemon thread. The Java Virtual Machine will exit when all runing threads are daemon threads.

Consider the following thread implementations.

WorkerThreadA.java

public class WorkerThreadA extends Thread {
    @Override
    public void run() {
        for (int count = 1; count <= 10; count++) {
            System.out.println(getName() + " prints: " + count);
        }
    }
}

WorkerThreadB.java

public class WorkerThreadB extends Thread {
    @Override
    public void run() {
        for (int iteration = 1; iteration <= 100; iteration++) {
            System.out.println(getName() + " prints: " + iteration);
        }
    }
}

Main Method

public static void main(String[] args) {
    WorkerThreadA primaryWorker = new WorkerThreadA();
    WorkerThreadB secondaryWorker = new WorkerThreadB();

    primaryWorker.setName("Primary");
    secondaryWorker.setName("Secondary");

    // Mark the second thread as a daemon thread
    secondaryWorker.setDaemon(true);

    primaryWorker.start();
    secondaryWorker.start();
}

Key Behavior: Daemon threads will terminate gradually once all non-daemon (user) threads have finished execution. In the analogy used, when the "Primary" thread completes, the "Secondary" (daemon) thread loses its purpose and is signaled to terminate. This termination is not instantaneous; the JVM allows the daemon thread a short period to finish its current work, resulting in a gradual stop.

In the example, WorkerThreadB has a loop of 100 iterations but may only print up to a number like 16 before the JVM exits after WorkerThreadA finishes. The exact count can vary between runs.

Sample Console Output:

Secondary prints: 1
Primary prints: 1
Secondary prints: 2
Primary prints: 2
Secondary prints: 3
Primary prints: 3
Secondary prints: 4
Primary prints: 4
Secondary prints: 5
Primary prints: 5
Secondary prints: 6
Primary prints: 6
Secondary prints: 7
Primary prints: 7
Secondary prints: 8
Primary prints: 8
Secondary prints: 9
Primary prints: 9
Secondary prints: 10
Primary prints: 10
Secondary prints: 11
Secondary prints: 12
Secondary prints: 13
Secondary prints: 14
Secondary prints: 15
Secondary prints: 16

Practical Use Case: Consider an instant messaging application. One thread handles the chat interface (Thread1), while another manages file transfer (Thread2). If the user closes the chat window, terminating Thread1, the file transfer Thread2 becomes unnecessary. By configuring Thread2 as a daemon thread, it will automatically terminate when the main chat thread ends, ensuring proper resource cleanup and application exit.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.