Java review--daemon threads and thread monitoring tools

Source: Internet
Author: User


1. Daemon Thread

The Java threading mechanism is divided into two types, the user thread and the daemon thread (Daemon thread).

User threads: Runs in the foreground, performing specific tasks. For example: The main thread of the program, the child thread that connects the network.

Daemon Thread: Running in the background, serving other threads. Example: Garbage collector thread

Thread similarities and differences: Once all the user threads are running, the daemon exits with the JVM, which is actually the life cycle of the daemon, because he is the thread that serves its user thread, and when the user thread executes, the role of the daemon ends and deserve exits! However, the user thread is not exited at the end of the main thread.

Thread application: The user thread is generally performed on a specific task, the daemon thread typically uses the monitoring thread in the database connection pool, the JVM starts monitoring threads, and so on.



2. API
thread T1 = new thread (sample); T1.setdaemon (true); T1.start ();

1) Thread.setdaemon (true) must be set before Thread.Start () or run out of a illegalthreadstateexception exception. In other words, you cannot set a running regular thread as a daemon thread.

2) The new thread generated in the daemon thread is also daemon.

3) Note that the read-write operation or the calculation logic cannot use the daemon thread. The virtual machine may have exited because the daemon thread has not yet arrived and is operating.


3. Thread Monitoring Tool

1) Use Jstack to generate thread snapshots in the bin directory of the JDK

2) Use of the Jstat.exe command-line tool, in the bin directory of the JDK

3) Use of the Jvisualvm.exe interface tool, provided by Sun, also in the bin directory of the JDK

These files are available in the bin directory of the JDK installation

The Jstack-l PID is executed through the DOS window to see the running state of the thread, and it is important to note the six states of the thread's life cycle.

Here the PID can be viewed by the Task Manager----View--Select column--check PID

A thread state. A thread can is in one of the following states:

    • NEW
      A thread that has not yet started are in this state.

    • RUNNABLE
      A thread executing in the Java virtual machine was in this state.

    • BLOCKED
      A thread that's blocked waiting for a monitor lock are in this state.

    • WAITING
      A thread that's waiting indefinitely for another the thread to perform a particular action was in the this state.

    • TIMED_WAITING
      A thread that's waiting for another the thread to perform an action for up to a specified waiting time was in this state.

    • TERMINATED
      A thread that has exited is in the this state.

A thread can is in the only one state at a given point in time. These states is virtual machine States which don't reflect any operating system thread states.


Using Jvisualvm.exe to view the status of the thread, as long as the interface program is started, the program will automatically search the Java program, and monitor the use of threads



Java review--daemon threads and thread monitoring tools

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.