Java Multithreading (10)--Thread priority and daemon thread

Source: Internet
Author: User

I. Introduction to Thread Priority

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 are typically used to 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:

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.

Second, Thread priority example

Let's take a look at the priority example:

 Packagecom.demo.threadPriority; Public classMyThreadextendsthread{ PublicMyThread (String name) {Super(name); }     Public voidrun () { for(inti=0; i<5; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ "(" +thread.currentthread (). GetPriority () + ")" + ", loop" +i); }    } }
 Packagecom.demo.threadPriority; Public classDemo { Public Static voidMain (string[] args) {System.out.println (Thread.CurrentThread (). GetName ()+ "(" +thread.currentthread (). GetPriority () + ")"); Thread T1=NewMyThread ("T1");//New T1Thread t2=NewMyThread ("T2");//New T2T1.setpriority (1);//set the T1 priority to 1T2.setpriority (10);//set the T2 priority to tenT1.start ();//Start T1T2.start ();//Start T2    }  }

Operation Result:

Main (5) T1 (1), loop 0T1 (1), Loop 1T2 (Ten), loop 0T1 (1), loop 2T2, Loop 1< C7>T1 (1), Loop 3T2 (Ten), loop 2T1 (1), Loop 4T2 (Ten), Loop 3T2 (), 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.

Iii. example of a daemon thread

The following is an example of a daemon thread.

 PackageCom.demo.daemonThread; Public classMyThreadextendsthread{ PublicMyThread (String name) {Super(name); }     Public voidrun () {Try {             for(inti=0; i<5; i++) {Thread.Sleep (3); System.out.println ( This. GetName () + "(isdaemon=" + This. Isdaemon () + ")" + ", loop" +i); }        } Catch(Interruptedexception e) {} }}
 PackageCom.demo.daemonThread; Public classMydaemonextendsthread{ PublicMydaemon (String name) {Super(name); }     Public voidrun () {Try {             for(inti=0; i<10000; i++) {Thread.Sleep (1); System.out.println ( This. GetName () + "(isdaemon=" + This. Isdaemon () + ")" + ", loop" +i); }        } Catch(Interruptedexception e) {} }}
 PackageCom.demo.daemonThread; Public classDemo { Public Static voidMain (string[] args) {System.out.println (Thread.CurrentThread (). GetName ()+ "(isdaemon=" +thread.currentthread (). Isdaemon () + ")"); Thread T1=NewMyThread ("T1");//New T1Thread t2=NewMydaemon ("T2");//New T2T2.setdaemon (true);//set T2 as the daemon threadT1.start ();//Start T1T2.start ();//Start T2    }  }

Operation Result:

Main (isdaemon=false) T2 (Isdaemon=true), Loop 0T2 (Isdaemon=true), Loop 1T1 (Isdaemon=false), Loop 0T2 (Isdaemon=true), Loop 2T1 (Isdaemon=false), Loop 1T2 (Isdaemon=true), Loop 3T2 (Isdaemon=true), Loop 4T1 (Isdaemon=false), Loop 2T2 (Isdaemon=true), Loop 5T2 (Isdaemon=true), Loop 6T1 (Isdaemon=false), Loop 3T2 (Isdaemon=true), Loop 7T2 (Isdaemon=true), Loop 8T2 (Isdaemon=true), Loop 9T1 (Isdaemon=false), Loop 4

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 Multithreading (10)--Thread priority and daemon thread

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.