Threads (9)--priority of threads and background threads

Source: Internet
Author: User
Tags thread class

Priority of Threads:
1. In Java, the priority level of the thread is defined 10 levels, range: 1 (thread.min_priority) ~ (thread.max_priority), but different systems have different thread priority range of values
2. The default priority for the Thread class is 5 (thread.norm_priority)
3. Set thread priority using the SetPriority (int) method
A small case to show thread priority:
Fatherthread.java

Package com.poriority.entity;
/**
 * @version 2018-1-2 a.m. 8:43:54 * * Public

class Fatherthread extends thread{public
    fatherthread () c7/>//sets the priority of the class, the priority of the class needs to be set by the constructor, and the priority that is set here affects the priority of the subclass
        This.setpriority (8);
    }

Sonthread.java

Package com.poriority.entity;
/**
 * @version 2018-1-2 Morning 8:53:04 * * Public
class Sonthread extends fatherthread{
    // The precedence of subclasses inherits from the precedence of the parent class
}

Testpriority.java

package com.poriority.test; import com.poriority.entity.FatherThread; import
Com.poriority.entity.SonThread; /** * @version 2018-1-2 a.m. 8:45:02/public class Testpriority {public static void main (string[] args) {Th
        Read thread = new thread ();

        SYSTEM.OUT.PRINTLN ("The priority of the default thread is:" + thread.getpriority ());
        Fatherthread ft = new Fatherthread ();
        The priority of the default thread is the priority of inheriting the parent class System.out.println ("Fatherthread Thread priority is:" + ft.getpriority ());
        The priority set here is used only to set the priority of the current object and does not affect ft.setpriority (9) for the precedence of the subclass;


        System.out.println ("Fatherthread thread setting Priority is:" + ft.getpriority ());
        Sonthread st = new Sonthread ();

    The default priority of subclasses is to inherit the precedence of the parent class System.out.println ("Sonthread Thread priority is:" + st.getpriority ()); }
}

Thread-Priority summary:
The default priority of a subclass thread is to inherit the priority of the direct parent thread, and the object's precedence is implemented through the object. Serpriority (int) method, and the class priority is implemented by constructing the method. setpriority (int).
Background Thread:
1. Run in the background of the system without direct interaction with the user; Such a thread is called a background thread (also known as a service thread or daemon).
– for example: file downloads
2. Setdaemon () Method: Set the thread as a background thread
A small case for displaying background threads:
Fatherthread.java

Package com.poriority.entity;

/**
 * @version 2018-1-2 a.m. 8:43:54 * * Public

class Fatherthread extends thread{public
    fatherthread () c8/>//sets the priority of a class, the priority of the class is set by constructing a method, and the priority that is set here affects the priority of the subclass
        This.setpriority (8);
    }
    Constructs a method that sets the name of the thread public
    fatherthread (String name) {
        super (name);
    }

    Thread
    @Override public
    void Run () {
        try {
            thread.sleep (100000);
        } catch ( Interruptedexception e) {
            e.printstacktrace ();}}}

Sonthread.java

Package com.poriority.entity;
/**
 * @version 2018-1-2 a.m. 8:53:04

/public class Sonthread extends fatherthread{
    //Subclass priority inherits from parent class precedence

    //thread
        @Override public
        void Run () {
            try {
                thread.sleep (100000);
            } catch ( Interruptedexception e) {
                e.printstacktrace ();}}}

Testthread.java

package com.poriority.test; import com.poriority.entity.FatherThread; import

Com.poriority.entity.SonThread;
        /** * @version 2018-1-2 a.m. 8:45:02/public class Testpriority {public static void main (string[] args) {
        Thread thread = new Thread ();

        SYSTEM.OUT.PRINTLN ("The priority of the default thread is:" + thread.getpriority ());
        Fatherthread ft = new Fatherthread ("father");
        The priority of the default thread is the priority of inheriting the parent class System.out.println ("Fatherthread Thread priority is:" + ft.getpriority ());
        The priority set here is used only to set the priority of the current object and does not affect ft.setpriority (9) for the precedence of the subclass;
        Ft.start ();


        System.out.println ("Fatherthread thread setting Priority is:" + ft.getpriority ());
        Sonthread st = new Sonthread ();
        Sets the subclass thread as a background thread, the parameter is Boolean, and True indicates that the setting is a background thread, false indicates that it is not set as a background thread, and the background thread setting must be set St.setdaemon (true) before the thread starts;
        St.start ();

    The default priority of subclasses is to inherit the precedence of the parent class System.out.println ("Sonthread Thread priority is:" + st.getpriority ()); }

}

The

Background Thread summary
Background Thread setting method is the Setdaemon () method, which is set before the thread is started, and the method of viewing the background thread is viewed as stepping, and the method of naming the line Chenghong is to use the constructor method, The method of calling the parent class in the constructor is renamed.

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.