Java Multithreading Core Technology (note)-Thread priority

Source: Internet
Author: User

Introduction: In the operating system, threads can prioritize, high priority to obtain more CPU resources, that is, the CPU priority to execute higher priority threads. Priority of Threads in Java

Divide 1~~10 10 levels. is greater than or less than the reported exception.

One, the thread priority has inheritance

A thread initiates a B thread, then thread B has the same priority as a.

 public  class  MyThread1 extends   Thread {@Override   void   run () {System.out.println ( "MyThread1 run priority=" + this  .getpriority (        ));        MyThread2 thread2  = new   MyThread2 ();    Thread2.start (); }}
 public  class  MyThread2 extends   Thread {@Override   void   run () {System.out.println ( "MyThread2 run priority=" + this  .getpriority (    )); }}
 Public class Run {    publicstaticvoid  main (string[] args) {        System.out.println ( "Main thread begin priority="                + Thread.CurrentThread (). getpriority ());  \ \ Gets the priority        of the thread  // Thread.CurrentThread (). setpriority (6);           \ \ Sets the priority of the thread        System.out.println ("Main thread end   priority="                + Thread.CurrentThread (). GetPriority ());         New MyThread1 ();        Thread1.start ();    }}

Results:

Second, thread priority is rule-
 Public classMyThread1extendsThread {@Override Public voidrun () {LongBeginTime =System.currenttimemillis (); LongAddresult = 0;  for(intj = 0; J < 10; J + +) {             for(inti = 0; I < 50000; i++) {Random random=NewRandom ();                Random.nextint (); Addresult= Addresult +i; }        }        LongEndTime =System.currenttimemillis (); System.out.println ("★★★★★thread 1 Use time=" + (EndTime-beginTime)); }}
 Public classMyThread2extendsThread {@Override Public voidrun () {LongBeginTime =System.currenttimemillis (); LongAddresult = 0;  for(intj = 0; J < 10; J + +) {             for(inti = 0; I < 50000; i++) {Random random=NewRandom ();                Random.nextint (); Addresult= Addresult +i; }        }        LongEndTime =System.currenttimemillis (); System.out.println ("☆☆☆☆☆thread 2 Use Time=" + (EndTime-beginTime)); }}

 public  class   Run { public  static  void   main (string[] args) { for  (int  i = 0; I < 5; I++) {MyThread1 Thread1  = new   MyThread1 ();            Thread1.setpriority ( 1 = new   MyThread2 ();            Thread2.setpriority ( 10
The result: high-priority threads are always executed mostly first, run faster, and do not mean that the thread's priority is independent of the order in which the threads are executed. This results in a 10 times-fold difference in priority. Change to a similar priority then look, like.

Third, thread priority has randomness

Multiple runs of code with similar priority, you will find the following results appear: You can verify that the priority of a thread is random.

Daemon Thread (daemon)

Concept: Any daemon thread is a "nanny" for all non-daemon threads throughout the JVM. The daemon thread is working as long as there is a non-daemon thread in the current instance that does not end. When the last non-daemon thread ends

, the daemon thread will not end up working. (Typical, GC garbage collector)

Java Multithreading Core Technology (note)-Thread 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.