Java Multithreading: Thread priority and daemon thread for "base piece" 10

Source: Internet
Author: User
Tags exit thread

1. 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 kinds of threads in Java: The user thread and the daemon thread. They can be distinguished by the Isdaemon () method: If returned false, the thread is a user thread;

User threads typically perform user-level tasks, and daemons are "background threads" that are typically used to perform background tasks. Note that the Java virtual machine exits after the user thread has finished.

The introduction to thread priority and daemon threads in JDK is as follows:

Every thread has a priority. Threads with higher priority are executed into preference to Threads with lower. Each thread may or could not also is marked as a daemon. When code running in some thread creates a new thread object, the new thread has it priority initially set equal to T
    
He priority of the creating thread, and are a daemon thread if and only if the creating thread is a daemon. When a Java Virtual Machine starts up, there be usually a single non-daemon thread (which typically calls the method Nam Ed main of some designated Class). The Java Virtual Machine continues to execute threads until either of the following Occurs:the exit method of Clas
S Runtime has been called and the security manager has permitted the exit operation to take place.  All threads this are not daemon threads have died, either by returning from the "call" to "run" or by throwing a 
Exception that propagates beyond the run method. Marks this thread as either a daemon Thread or a user thread. The Java Virtual Machine exits when the "only" threads running are all daemon threads.

The General meaning is:

Each thread has a priority level. 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 of the running main threads, the priority of the child thread is set to equal to the priority of the primary thread that created it, and the child thread is the daemon only if the main thread that created it is a daemon thread.

When a Java virtual machine is started, there is usually a single, non-daemon thread that is started through the main () method. The JVM will run until any one of the following conditions occurs, which terminates:

(01) The exit () method is invoked, and exit () has permission to execute normally.

(02) All "Non-daemon Threads" are dead (that is, the JVM only has "daemon threads").

Each thread is marked as a "daemon" or "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 first.

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 of setting T1 is 1
        t2.setpriority (ten);                Set the priority of T2 to ten
        T1.start ();                        Start T1
        T2.start ();                        Start T2
    }  
}

Run results

Main (5)
T1 (1), Loop 0
T2, loop 0
T1 (1), loop 1
T2, loop 1
T1 (1), Loop 2
T2 (Ten), Loop 2
  
   T1 (1), Loop 3
T2 (Ten), loop 3
T1 (1), Loop 4
T2 (Ten), Loop 4
  

Results show:

(01) Main thread Main's priority is 5.

The T1 priority is set to 1, and the T2 priority is set to 10. When the CPU executes T1 and T2, it is able to execute concurrently according to the time cycle scheduling.

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.