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:
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 02" all "non-daemons" 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
Class MyThread extends thread{public MyThread (String name) { super (name); } public void Run () {for (int i=0; i<5; i++) { System.out.println (Thread.CurrentThread (). GetName () + "(" + Thread.CurrentThread (). GetPriority () + ")" + ", loop" +i);}} ; public class Demo {public static void Main (string[] args) { System.out.println (Thread.CurrentThread ()). GetName () + "(" +thread.currentthread (). GetPriority () + ")"); Thread t1=new MyThread ("T1"); New T1 Thread t2=new MyThread ("T2"); New T2 t1.setpriority (1); The priority for setting the T1 is 1 t2.setpriority (ten); The priority of setting T2 is t1.start (); Start T1 T2.start (); Start T2 } }
Run Results:
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.
Demo.javaclass MyThread extends thread{public MyThread (String name) {super (name); public void Run () {try {for (int i=0; i<5; i++) {thread.sleep (3); System.out.println (This.getname () + "(isdaemon=" +this.isdaemon () + ")" + ", loop" +i "; }} catch (Interruptedexception e) {}}}; Class Mydaemon extends thread{public Mydaemon (String name) {super (name); public void Run () {try {for (int i=0; i<10000; i++) {thread.sleep (1); System.out.println (This.getname () + "(isdaemon=" +this.isdaemon () + ")" + ", loop" +i "; }} catch (Interruptedexception e) {}}}public class Demo {public static void main (string[] args) {System.out.println (Thread.CurrentThread (). GetName () + "(isdaemon=" +thread.currentthread (). Isdaem On () + ")"); Thread t1=new MyThread ("T1"); New T1 Thread t2=new Mydaemon ("T2"); New T2 T2.setdaemon (true); Set T2 as the daemon thread T1.start (); Start T1 t2.start (); Start T2}}
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.
Attached: What is a daemon thread
The daemon thread is not available inside the virtual machine, and the user can set the daemon thread by itself: public final void Setdaemon (Boolean on), but there are a few things to note:
1), Thread.setdaemon (true) must be set before Thread.Start (), or a illegalthreadstateexception exception will run out. You cannot set a running regular thread as a daemon thread. (Note: This is a significant difference from the daemon, the daemon is created, let the process out of the original session control + let the process out of the original Process group Control + Let the process out of the original control terminal control; So the language mechanism pinned to the virtual machine is fundamentally different from the system-level language)
2), the new thread generated in the daemon thread is also daemon. (This is the essential difference: the Daemon fork () is no longer the daemon, although it copies the process-related information of the parent process, but the process of the child process is not the INIT process, the so-called Daemon is essentially "the parent process hangs, init adoption, Then file 0,1,2 are/dev/null, current directory to/")
3), not all applications can be assigned to daemon threads for service, such as read-write operations or computational logic. The virtual machine may have exited because the daemon thread has not come and is operating.
For example, a servlet in a Web server that initializes a service thread in the background when the container starts, that is, the dispatch thread , is responsible for processing the HTTP request, and then each request comes up to the dispatch thread to take a worker thread out of the thread pool to handle the request for concurrency control.
Online pick of a map, easy to understand:
Java Multithreading--priority