JAVA thread scheduling priority, java Thread Scheduling

Source: Internet
Author: User

JAVA thread scheduling priority, java Thread Scheduling

The Thread priority is represented by numbers. The default range is 1 to 10, that is, Thread. MIN_PRIORITY to Thread. MAX_PRIORTY. The default priority of a Thread is 5, that is, Thread. NORM_PRIORTY.

Method for priority operation:

Int getPriority (): gets the thread priority.

Void setPriority (int newPriority): After a thread is created, you can use this method to change the thread priority.

It must be noted that the thread priority cannot guarantee the thread execution order. However, threads with a higher priority have a higher probability of obtaining CPU resources.

Class MyThread extends Thread {String message; MyThread (String message) {this. message = message;} public void run () {for (int I = 0; I <3; I ++) {System. out. println (message + "" + getPriority () ;}} public class PriorityThread {public static void main (String args []) {Thread t1 = new MyThread ("T1"); t1.setPriority (Thread. MIN_PRIORITY); t1.start (); Thread t2 = new MyThread ("T2"); t2.setPriority (Thread. MAX_PRIORITY); t2.start (); Thread t3 = new MyThread ("T3"); t3.setPriority (Thread. MAX_PRIORITY); t3.start ();}}

 

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.