Java-Thread priority and daemon threads

Source: Internet
Author: User

Java Multithreading Series--"Basic article" 10 Thread priority and daemon thread

Overview

In this chapter, the daemon threads and thread priorities are described. The topics covered include:
1. Introduction to Thread prioritization
2. Example of Thread priority
3. Examples of daemon threads

Reprint Please specify source:http://www.cnblogs.com/skywang12345/p/3479982.html

1. Introduction to Thread prioritization

The range of thread priorities in Java is 1~10, and the default priority is 5. High-priority threads take precedence over low-priority threads.

There are two types of threads in Java: The user thread and the daemon thread . They can be distinguished by the Isdaemon () method: If False is returned, the thread is a "user thread" or "Daemon thread".
User threads typically perform user-level tasks, while daemons, which are "background threads", are typically used to perform background tasks. It is important to note that the Java virtual machine exits after the user thread has finished.

The thread priority and daemon threads in the JDK are described below:

Every thread has a priority. Threads with higher priority is executed in preference to Threads with lower priority. Each thread may or may not also is marked as a daemon. When code running in some thread creates a new thread object, the new thread have its priority initially set equal to the P Riority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.  When a Java Vsan starts up, there are usually a single non-daemon thread (which typically calls the method named Main of some designated Class). The Java Virtual machine continues to execute threads until either of the following Occurs:the exit method of Class Runtim E has been called and the security Manager have permitted the exit operation to take place. All threads is not daemon threads has died, either by returning from the call to the Run method or by throwing an E Xception that propagates beyond the run method. Marks this thread as either a daemon thread or a user thread. The JAva Virtual Machine Exits if the only threads running is all daemon threads. 

The General meaning is:

Each thread has a priority. High-priority threads take precedence over low-priority threads. Each thread can be marked as a daemon or a non-daemon process. When a new child thread is created in some running main thread, the child thread's priority is set equal to the priority of the main thread that created it, and the child thread is the daemon only when the main thread that created it is the daemon thread. When a Java virtual machine is started, there is usually a single non-daemon thread (the thread is started through the main () method). The JVM will run until any of the following conditions occur and the JVM will terminate: (01) The exit () method is called and exit () has permission to execute normally. (02) All "Non-daemon Threads" are dead (that is, only "daemon threads" in the JVM). Each thread is marked as either a daemon thread or a user thread. The JVM automatically exits when only the daemon thread is running.

2. Example of thread priority

Let's take a look at the priority example

1 class MyThread extends thread{   2 public     MyThread (String name) {3         super (name), 4     } 5  6 public     V OID Run () {7 for         (int i=0; i<5; i++) {8             System.out.println (Thread.CurrentThread (). GetName () 9                     + "(" + Thread.CurrentThread (). GetPriority () + ")"                     + ", loop" +i);         }12     } 13}; public class Demo {+ public     static void Main (string[] args) {         System.out.println (Thread . CurrentThread (). GetName ()                 + "(" +thread.currentthread (). GetPriority () + ")");         Thread t1=new MyThread ("T1");    New t122         Thread t2=new MyThread ("T2");    New t223         t1.setpriority (1);                The priority of setting T1 is 124         t2.setpriority (ten);                Set the T2 priority to 1025         T1.start ();                        Start t126         t2.start ();                        Start t227     }  28}

Operation result :

Main (5) T1 (1), Loop 0t2 (Ten), Loop 0t1 (1), Loop 1t2 (Ten), Loop 1t1 (1), Loop 2t2 (Ten), Loop 2t1 (1), Loop 3t2 (Ten), Loop 3t1 (1), Loop 4t2 (Ten), Loop 4

Result Description :
(01) The priority of main thread main is 5.
The priority of the T1 is set to 1, and the T2 priority is set to 10. When the CPU executes T1 and T2, it can execute concurrently according to the time-slice rotation schedule.

3. Examples of daemon threads

The following is an example of a daemon thread.

 1//Demo.java 2 class MyThread extends thread{3 public MyThread (String name) {4 super (name); 5} 6                 7 public void Run () {8 try {9 for (int i=0; i<5; i++) {thread.sleep (3); 11         System.out.println (This.getname () + "(isdaemon=" +this.isdaemon () + ")" + ", loop" +i "; 12}13 } catch (Interruptedexception e) {14}15} 16};  Mydaemon extends thread{public Mydaemon (String name) {+ super (name);}22                 void run () {try {i=0; i<10000; i++) {thread.sleep (1); 27 System.out.println (This.getname () + "(isdaemon=" +this.isdaemon () + ")" + ", loop" +i ",}29} cat CH (interruptedexception e) {}31}}33 public class Demo {The public static void main (string[] args          ) {System.out.println (Thread.CurrentThread (). GetName () 37       + "(isdaemon=" +thread.currentthread (). Isdaemon () + ")"), and the T1=new MyThread ("T1");    New t140 Thread t2=new mydaemon ("T2");                New t241 T2.setdaemon (true);                        Set T2 as the daemon thread T1.start ();                        Start t143 T2.start (); Start t244} 45}

Operation result :

Main (Isdaemon=false) T2 (isdaemon=true), Loop 0t2 (isdaemon=true), Loop 1t1 (Isdaemon=false), Loop 0t2 (isdaemon=true), Loop 2t2 (isdaemon=true), Loop 3t1 (Isdaemon=false), Loop 1t2 (isdaemon=true), Loop 4t2 (isdaemon=true), Loop 5t2 (isdaemon= true), Loop 6t1 (Isdaemon=false), Loop 2t2 (isdaemon=true), Loop 7t2 (isdaemon=true), Loop 8t2 (isdaemon=true), Loop 9t1 ( Isdaemon=false), Loop 3t2 (isdaemon=true), Loop 10t2 (isdaemon=true), Loop 11t1 (Isdaemon=false), Loop 4t2 (isdaemon=true) , Loop 12

Result Description :
(01) The main thread is the user thread, and the child thread that it creates is T1 also the user thread.
(T2) is a daemon thread. The JVM exits automatically when main thread main and child thread T1 (both of which are user threads) are executed, leaving only the daemon T2.

Java-Thread priority and daemon threads

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.