The priority of threads in Java

Source: Internet
Author: User

Introduction to Java Thread Priority:

1. Java provides a thread scheduler to monitor all threads in the program that have entered a ready state after being started.

Determines which thread should be dispatched to execute according to the priority of the thread.

2. The priority of a thread is represented by a number, ranging from 1 to 10, and the default priority for a thread is 5.

thread.min_priority = 1;

Thread.max_priority = 10;

thread.norm_priority = 5;

3. Use the following method to get or set the priority of the thread object.

int getpriority ();

void setpriority (int newpriority);

"Note" The higher the priority of the thread, the more time slices will be executed by the CPU. (Each thread has its own priority)

Demo_1:

public class Testpriority {public static void main (string[] args) {T1 T1 = new T1 (); T2 t2 = new T2 (); Thread tt1 = new Thread (t1); Thread tt2 = new Thread (t2); Tt1.setpriority (thread.norm_priority + 3); The priority of the TT1 is increased, the time slice that gets CPU executes is tt1.start (); Tt2.start ();}} Class T1 implements Runnable {@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.println ("T1:" +i);} }class T2 implements Runnable {@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.println ("---T2:" +i) ;}}}

Demo_2:

The priority of threads in Java

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.