[Java Concurrent Programming]: Four Situations of Daemon Thread and Thread Blocking

daemon thread

  There are two types of threads in Java: User Thread (user thread), Daemon Thread (guardian thread) 

     1. Daemon thread A daemon thread is a special thread whose function is to provide services for other threads. When all non-daemon threads are terminated, daemon threads are automatically terminated as well. Daemon threads are usually used to perform some background tasks, such as garbage collection, timing tasks, etc.

    2. Four cases of thread blocking Thread blocking means that a thread is suspended during execution and waits for a certain condition to be met before continuing to execute. There are four common thread blocking situations:

(1) Waiting for blocking: The thread calls the wait() method and enters the waiting state until other threads call the notify() or notifyAll() method to wake it up.

(2) Synchronous blocking: The thread is blocked when acquiring the lock until the lock is released.

(3) Sleep blocking: The thread calls the sleep() method, enters the sleep state, and automatically wakes up after waiting for a certain period of time.

(4) I/O blocking: The thread is blocked when performing an I/O operation, and waits for the I/O operation to complete before continuing to execute.

    The above four situations will cause the thread to suspend execution until a certain condition is met before continuing. When writing multi-threaded programs, you need to pay attention to these blocking situations to avoid problems such as deadlocks.

     User threads are threads running in the foreground, while daemon threads are threads running in the background. The role of the daemon thread is to provide convenient services for the operation of other foreground threads, and it is only needed when ordinary, non-daemon threads are still running. For example, the garbage collection thread is a daemon thread. When the VM detects that there is only one daemon thread left, and all user threads have exited, the VM will exit, because there is no need to continue running the program if there is no daemon thread left. If there are non-daemon threads still alive, the VM will not exit.

     The daemon thread is not only provided inside the virtual machine, and the user can also set the daemon thread by himself when writing a program. Users can use Thread's setDaemon(true) method to set the current thread as a daemon thread.

    While a daemon thread can be very useful, care must be taken to ensure that no harm is done by its termination when all other non-daemon threads die. Because it is impossible for you to know whether the daemon thread has completed the expected service task before all user threads exit running. Once all user threads exit, the virtual machine also exits. Therefore, do not perform business logic operations (such as reading and writing data, etc.) in the daemon thread. ,

    There are a few more points to note:

    1. setDaemon(true) must be set before calling the start() method of the thread, otherwise an IllegalThreadStateException will be thrown.

    2. The new thread generated in the daemon thread is also a daemon thread.  
    3. Don't think that all applications can be assigned to daemon threads for services, such as read and write operations or calculation logic. 

    Threads can be blocked in four states:

    1. When a thread executes Thread.sleep(), it blocks until the specified millisecond time, or the blocking is interrupted by another thread;

    2. When a thread encounters a wait() statement, it will block until it receives a notification (notify()), is interrupted, or the specified millisecond time passes (if a timeout value is specified)

    3. There are many ways of thread blocking and different I/O. A common way is InputStream's read() method, which blocks until one byte of data is read from the stream. It can block infinitely, so the timeout cannot be specified;

    4. Threads can also be blocked waiting to acquire exclusive access to an object lock (ie, blocked while waiting to acquire the lock necessary for a synchronized statement).

    Note that not all blocking states are interruptible, the first two of the above blocking states can be interrupted, and the latter two will not respond to interruptions

Guess you like

Origin blog.csdn.net/gaowenhui2008/article/details/49760011