Java-18.2 basic thread mechanism (4)-priority

Source: Internet
Author: User

Java-18.2 basic thread mechanism (4)-priority

In this section, we will discuss the priority of multithreading.

1. Example:

package com.ray.ch17;public class Test {public static void main(String[] args) {MyThead myThead = new MyThead();MyThead2 myThead2 = new MyThead2();for (int i = 0; i < 5; i++) {Thread thread = new Thread(myThead);thread.start();thread.setPriority(Thread.MIN_PRIORITY);}Thread thread2 = new Thread(myThead2);thread2.start();thread2.setPriority(Thread.MAX_PRIORITY);}}class MyThead implements Runnable {public void run() {for (int i = 1; i <= 3; i++) {System.out.println(Thread.currentThread() + "thread======>AAA");}}}class MyThead2 implements Runnable {public void run() {for (int i = 1; i <= 3; i++) {System.out.println(Thread.currentThread() + "thread======>BBB");}}}
Output:

Thread [Thread-5, 10, main] thread ====> BBB
Thread [Thread-5, 10, main] thread ====> BBB
Thread [Thread-5, 10, main] thread ====> BBB
Thread [Thread-0, 1, main] thread ====> AAA
Thread [Thread-0, 1, main] thread ====> AAA
Thread [Thread-0, 1, main] thread ====> AAA
Thread [Thread-2, 1, main] thread ====> AAA
Thread [Thread-3, 1, main] thread ====> AAA
Thread [Thread-3, 1, main] thread ====> AAA
Thread [Thread-3, 1, main] thread ====> AAA
Thread [Thread-1, 1, main] thread ====> AAA
Thread [Thread-1, 1, main] thread ====> AAA
Thread [Thread-1, 1, main] thread ====> AAA
Thread [Thread-2, 1, main] thread ====> AAA
Thread [Thread-2, 1, main] thread ====> AAA
Thread [Thread-4, 1, main] thread ====> AAA
Thread [Thread-4, 1, main] thread ====> AAA
Thread [Thread-4, 1, main] thread ====> AAA

 

As you can see from the output, priority is at least effective in my system.

Explanation:

(1) The priority can be set through setPriority.

(2) the time set by setPriority should be after start. If it is too high, it is possible that the task has not started.

(3) The priority is generally divided into three levels: max, min, and norm.

/**     * The minimum priority that a thread can have.      */    public final static int MIN_PRIORITY = 1;   /**     * The default priority that is assigned to a thread.      */    public final static int NORM_PRIORITY = 5;    /**     * The maximum priority that a thread can have.      */    public final static int MAX_PRIORITY = 10;

2. implicit question of priority (the following is from the http://silentlakeside.iteye.com/blog/1175981)

The following content is excerpted and translated from JAVAMEX-> Java threading introduction-> Thread priorioties
For thread priority, note the following:
* Thread. setPriority () may not do anything at all, which is related to the operating system and virtual machine version.
* The thread priority may have different meanings for different thread schedulers, which may not be directly guessed by you. In particular, priority does not necessarily mean CPU sharing. In UNIX systems, the priority can be considered as CPU allocation, but Windows does not.
* The thread priority is generally a combination of global and local priority settings. Java's setPriority () method applies only to local priorities. In other words, you cannot set a priority across the entire range of possibilities. (This is usually a protection method. You probably do not want the mouse pointer thread or the thread that processes audio data to be preemptible by other random user threads)
* Different systems have different thread priority values, but Java defines 10 levels (1-10 ). In this way, several threads may have different priorities in one operating system, but they have the same priority in another operating system (and therefore may have unexpected behaviors)
* The operating system may (and normally) add some proprietary behaviors to the thread Based on the thread priority (for example, "only give a quantum boost if the priority is below X"). Here we repeat it again. The definition of priority is partially different between different systems.
* In most operating systems, the thread scheduler actually executes temporary operations on the priority of a thread from a strategic perspective (for example, when a thread receives an event or I/ O ), generally, the operating system knows the most, and attempts to manually control the priority may only interfere with this system.
* Your application generally does not know which threads are run by other processes. Therefore, it is difficult to predict the impact of changing the priority of a thread for the entire system. For example, you may find that you have a low-priority thread that is expected to run occasionally in the background and has almost no running, the reason is that a virus monitoring program runs on a slightly higher priority (but still lower than the normal priority) and cannot predict the performance of your program, it varies according to the anti-virus programs used by your customers.

You can refer to the correspondence between Java priorities and operating system priorities.

In short, priority is determined by different systems.

Summary: This section describes the usage of priority.

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.