"Java Concurrency Programming": Four Scenarios for daemon thread and thread blocking

Source: Internet
Author: User

Daemon Threads


There are two types of threads in JAVA:user thread, Daemon thread (daemon thread)

The user thread is the thread that is running in the foreground, and the daemon thread is the thread running in the background. The role of the daemon is to facilitate the operation of other foreground threads and is only required when the normal, non-daemon threads are still running, such as a garbage collection thread that is a daemon thread. When the VM detects only one daemon thread, and the user thread exits the runtime, the VM exits because there is no need to continue running the program without being guarded. If a non-daemon thread is still alive, the VM will not exit.

The daemon thread is not only available inside the virtual machine, but the user can also set the daemon itself when writing the program. The user can set the current thread as the daemon thread by using the thread's Setdaemon (True) method.

While the daemon may be useful, care must be taken to ensure that all other non-daemon threads die without any harm from its termination. because you cannot know whether the daemon has completed the expected service task before all the user threads are out of operation. Once all the user threads have exited, the virtual machine exits and runs. Therefore, do not perform business logic operations (such as reading and writing data) in the daemon thread. ,

There are several other points to note:

1, Setdaemon (true) must be set before the start () method of the calling thread, or the illegalthreadstateexception exception will run out.

2. The new thread that is generated in the daemon thread is also the daemon thread.
3. Do not assume that all applications can be assigned to the daemon for service, such as read-write operations or computational logic.

Thread Blocking

Threads can block in four states:

1, when the thread executes Thread.Sleep (), it blocks until the specified millisecond time, or the block is interrupted by another thread;

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

3, there are many ways to block the thread and different I/O. A common way is InputStream's read () method, which blocks until a byte of data is read from the stream, it can block indefinitely, and therefore cannot specify a time-out;

4, a thread can also block waiting for exclusive access to an object lock (that is, to wait for a lock that the synchronized statement must have blocked).

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 interrupts

"Java Concurrency Programming": Four Scenarios for daemon thread and thread blocking

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.