Priority of the thread

Source: Internet
Author: User

------------siwuxie095

Priority order Settings:

If nothing is set, the default is 5

The priority of threads can affect the order in which threads are executed, but not absolute

Code:

Package Com.siwuxie095.thread;

Class Myrun implements runnable{

Publicvoid run () {

For (int i = 0; i < 5; i++) {

Try {

// every time you print, Hibernate 1 seconds

Thread.Sleep (+);

// gets the current thread name

System.out.println (Thread.CurrentThread (). GetName () +"-"+i);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

}

}

}

public class ThreadPriority {

< Span style= "COLOR: #b26818" >public staticvoid main (string[] args) {

< Span style= "Color:blue" >// 3 Span style= "Font-family:microsoft Yahei UI; Background-color:white "> thread object, passing in anonymous object and string ( identify thread )

Thread t1=new thread (new Myrun (),"A");

Thread t2=new thread (new Myrun (),"B");

Thread t3=new thread (new Myrun (),"C");

< Span style= "Color:blue" >// t1- minimum, t2- OK, t3- Max

// the greater the priority, the more likely it will increase T3 first execution speed, that is, the first to grab the CPU Resources

// but it's not absolutely possible .

T1.setpriority (thread.min_priority);

T2.setpriority (thread.norm_priority);

T3.setpriority (thread.max_priority);

// start this 3 Threads

T1.start ();

T2.start ();

T3.start ();

}

}

Run at a glance:

"Made by siwuxie095"

Priority of the thread

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.