Java Foundation---->java Multi-threaded use (iv)

Source: Internet
Author: User

The priority of the thread tells the scheduler how important it is, and today we use the example to learn about precedence in Java Multi-threading.

Java Multi-Threading Priority One, Java Multi-threading in the case of precedence
 PackageCom.linux.huhx.thread1;ImportJava.util.Random;ImportJava.util.concurrent.TimeUnit; Public classPrioritythreadextendsThread {prioritythread (String name,intPriority ) {        Super(name);    SetPriority (priority); } @Override Public voidrun () { for(inti = 0; I < 10; i++) {            Try{System.out.println (Thread.CurrentThread (). GetName ()+ "," + i + ", priority =" +Thread.CurrentThread (). getpriority ()); TimeUnit.SECONDS.sleep (NewRandom (). Nextint (5)); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}

The subject class content of the test is as follows:

 Package Com.linux.huhx.thread1;  Public class prioritythreadtest {    publicstaticvoid  main (string[] args) {         New Prioritythread ("T-1", thread.norm_priority);         New Prioritythread ("T-2", thread.max_priority);        Thread1.start ();        Thread2.start ();    }}

The results of one run are as follows:

T-2,0, priority =TenT-1,0, priority =5T-2,1, priority =TenT-2,2, priority =TenT-1,1, priority =5T-1,2, priority =5T-1,3, priority =5T-2,3, priority =TenT-1,4, priority =5T-1,5, priority =5T-2,4, priority =TenT-2,5, priority =TenT-2,6, priority =TenT-1,6, priority =5T-2,7, priority =TenT-1,7, priority =5T-1,8, priority =5T-1,9, priority =5T-2,8, priority =TenT-2,9, priority =Ten

Although the order in which the processor handles the existing set of threads is indeterminate, if many threads are blocked and waiting to run, the scheduler will prefer to have the highest priority thread execute first.

Friendship Link
    • Blog about multithreading Priorities in Java: http://www.cnblogs.com/wuyudong/p/java-multithreading8.html

Java Foundation---->java Multi-threaded use (iv)

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.