"Thinkinginjava" 59, the execution of the thread when we can set the corresponding priority

Source: Internet
Author: User

/*** Book: Thinking in Java * Function: We can set the corresponding priority when executing the thread * file: simplepriorities.java* time: May 7, 2015 15:17:09* Author: cutter_point*/ Package Lesson21concurency;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; public class Simplepriorities implements runnable{private int countdown = 5;//Here we use volatile in order to ensure that no compiler optimizations are made to private Volatile double d;//volatile It is designed to modify variables that are accessed and modified by different threads, and if there is no volatile, it basically leads to the result: either you can't write multithreaded programs, or the compiler loses a lot of optimization opportunities. private int priority;//Set precedence public simplepriorities (int priorities) {this.priority = priority;} @Overridepublic String toString () {return thread.currentthread () + ":" + Countdown;} @Overridepublic void Run () {Thread.CurrentThread (). SetPriority (priority);//Set thread precedence//middle for some calculation while (true) {for ( int i = 0; I < 100000;  ++i) {D + = (Math.PI + MATH.E)/(double) i;if (i% = = 0) Thread.yield (); This indicates that the thread can be replaced}system.out.println (this),//Output the current task tostringif (--countdown = = 0)//when the count is reduced to 0, exit the task return;}} public static void Main (string[] args) {Executorservice exec = ExeCutors.newcachedthreadpool ();//Create a new connection pool for (int i = 0; i <; ++i)//Here The value is to be set according to the computer, if the computer performance is better, may not show priority, My computer is about 65 to 66 to fully reflect the priority of the 5 Exec.execute (new Simplepriorities (1));//Here the priority number of different system priority is different, so we only use the largest, smallest, And the middle of the Exec.execute (new Simplepriorities (10));//Here the priority number of the system is different, so we only use the largest, smallest, and middle Exec.shutdown ();}}

Output:

Thread[pool-1-thread-67,10,main]: 5
Thread[pool-1-thread-67,10,main]: 4
Thread[pool-1-thread-67,10,main]: 3
Thread[pool-1-thread-67,10,main]: 2
Thread[pool-1-thread-67,10,main]: 1
Thread[pool-1-thread-2,1,main]: 5
Thread[pool-1-thread-4,1,main]: 5
Thread[pool-1-thread-7,1,main]: 5
Thread[pool-1-thread-10,1,main]: 5
Thread[pool-1-thread-27,1,main]: 5
Thread[pool-1-thread-11,1,main]: 5
Thread[pool-1-thread-18,1,main]: 5
Thread[pool-1-thread-15,1,main]: 5
Thread[pool-1-thread-14,1,main]: 5
Thread[pool-1-thread-19,1,main]: 5
Thread[pool-1-thread-22,1,main]: 5
Thread[pool-1-thread-23,1,main]: 5
Thread[pool-1-thread-31,1,main]: 5
Thread[pool-1-thread-30,1,main]: 5
Thread[pool-1-thread-26,1,main]: 5
Thread[pool-1-thread-3,1,main]: 5
Thread[pool-1-thread-34,1,main]: 5
Thread[pool-1-thread-38,1,main]: 5
Thread[pool-1-thread-35,1,main]: 5
Thread[pool-1-thread-1,1,main]: 5
Thread[pool-1-thread-39,1,main]: 5
Thread[pool-1-thread-42,1,main]: 5
Thread[pool-1-thread-46,1,main]: 5
Thread[pool-1-thread-43,1,main]: 5
Thread[pool-1-thread-50,1,main]: 5
Thread[pool-1-thread-5,1,main]: 5
Thread[pool-1-thread-51,1,main]: 5
Thread[pool-1-thread-6,1,main]: 5
Thread[pool-1-thread-9,1,main]: 5
Thread[pool-1-thread-55,1,main]: 5
Thread[pool-1-thread-47,1,main]: 5
Thread[pool-1-thread-54,1,main]: 5
Thread[pool-1-thread-58,1,main]: 5
Thread[pool-1-thread-59,1,main]: 5
Thread[pool-1-thread-66,1,main]: 5
Thread[pool-1-thread-62,1,main]: 5
Thread[pool-1-thread-8,1,main]: 5
Thread[pool-1-thread-12,1,main]: 5
Thread[pool-1-thread-13,1,main]: 5
Thread[pool-1-thread-63,1,main]: 5
Thread[pool-1-thread-17,1,main]: 5
Thread[pool-1-thread-21,1,main]: 5
Thread[pool-1-thread-24,1,main]: 5
Thread[pool-1-thread-28,1,main]: 5
Thread[pool-1-thread-20,1,main]: 5
Thread[pool-1-thread-29,1,main]: 5
Thread[pool-1-thread-16,1,main]: 5
Thread[pool-1-thread-25,1,main]: 5
Thread[pool-1-thread-33,1,main]: 5
Thread[pool-1-thread-41,1,main]: 5
Thread[pool-1-thread-57,1,main]: 5
Thread[pool-1-thread-32,1,main]: 5
Thread[pool-1-thread-44,1,main]: 5
Thread[pool-1-thread-40,1,main]: 5
Thread[pool-1-thread-60,1,main]: 5
Thread[pool-1-thread-56,1,main]: 5
Thread[pool-1-thread-53,1,main]: 5
Thread[pool-1-thread-45,1,main]: 5
Thread[pool-1-thread-52,1,main]: 5
Thread[pool-1-thread-64,1,main]: 5
Thread[pool-1-thread-49,1,main]: 5
Thread[pool-1-thread-48,1,main]: 5
Thread[pool-1-thread-36,1,main]: 5
Thread[pool-1-thread-37,1,main]: 5
Thread[pool-1-thread-61,1,main]: 5
Thread[pool-1-thread-65,1,main]: 5
Thread[pool-1-thread-2,1,main]: 4
Thread[pool-1-thread-7,1,main]: 4
Thread[pool-1-thread-27,1,main]: 4
Thread[pool-1-thread-10,1,main]: 4
Thread[pool-1-thread-11,1,main]: 4
Thread[pool-1-thread-14,1,main]: 4
Thread[pool-1-thread-18,1,main]: 4
Thread[pool-1-thread-15,1,main]: 4
Thread[pool-1-thread-23,1,main]: 4
Thread[pool-1-thread-26,1,main]: 4
Thread[pool-1-thread-31,1,main]: 4
Thread[pool-1-thread-34,1,main]: 4
Thread[pool-1-thread-30,1,main]: 4
Thread[pool-1-thread-46,1,main]: 4
Thread[pool-1-thread-39,1,main]: 4
Thread[pool-1-thread-35,1,main]: 4
Thread[pool-1-thread-42,1,main]: 4
Thread[pool-1-thread-19,1,main]: 4
Thread[pool-1-thread-43,1,main]: 4
Thread[pool-1-thread-58,1,main]: 4
Thread[pool-1-thread-6,1,main]: 4
Thread[pool-1-thread-51,1,main]: 4
Thread[pool-1-thread-22,1,main]: 4
Thread[pool-1-thread-62,1,main]: 4
Thread[pool-1-thread-59,1,main]: 4
Thread[pool-1-thread-55,1,main]: 4
Thread[pool-1-thread-54,1,main]: 4
Thread[pool-1-thread-66,1,main]: 4
Thread[pool-1-thread-38,1,main]: 4
Thread[pool-1-thread-50,1,main]: 4
Thread[pool-1-thread-63,1,main]: 4
Thread[pool-1-thread-3,1,main]: 4
Thread[pool-1-thread-47,1,main]: 4
Thread[pool-1-thread-4,1,main]: 4
Thread[pool-1-thread-1,1,main]: 4
Thread[pool-1-thread-5,1,main]: 4
Thread[pool-1-thread-9,1,main]: 4
Thread[pool-1-thread-13,1,main]: 4
Thread[pool-1-thread-12,1,main]: 4
Thread[pool-1-thread-8,1,main]: 4
Thread[pool-1-thread-17,1,main]: 4
Thread[pool-1-thread-28,1,main]: 4
Thread[pool-1-thread-29,1,main]: 4
Thread[pool-1-thread-24,1,main]: 4
Thread[pool-1-thread-25,1,main]: 4
Thread[pool-1-thread-20,1,main]: 4
Thread[pool-1-thread-33,1,main]: 4
Thread[pool-1-thread-16,1,main]: 4
Thread[pool-1-thread-57,1,main]: 4
Thread[pool-1-thread-41,1,main]: 4
Thread[pool-1-thread-32,1,main]: 4
Thread[pool-1-thread-60,1,main]: 4
Thread[pool-1-thread-40,1,main]: 4
Thread[pool-1-thread-45,1,main]: 4
Thread[pool-1-thread-44,1,main]: 4
Thread[pool-1-thread-36,1,main]: 4
Thread[pool-1-thread-48,1,main]: 4
Thread[pool-1-thread-53,1,main]: 4
Thread[pool-1-thread-64,1,main]: 4
Thread[pool-1-thread-49,1,main]: 4
Thread[pool-1-thread-61,1,main]: 4
Thread[pool-1-thread-56,1,main]: 4
Thread[pool-1-thread-65,1,main]: 4
Thread[pool-1-thread-52,1,main]: 4
Thread[pool-1-thread-37,1,main]: 4
Thread[pool-1-thread-21,1,main]: 4
Thread[pool-1-thread-2,1,main]: 3
Thread[pool-1-thread-7,1,main]: 3
Thread[pool-1-thread-27,1,main]: 3
Thread[pool-1-thread-10,1,main]: 3
Thread[pool-1-thread-11,1,main]: 3
Thread[pool-1-thread-14,1,main]: 3
Thread[pool-1-thread-18,1,main]: 3
Thread[pool-1-thread-15,1,main]: 3
Thread[pool-1-thread-23,1,main]: 3
Thread[pool-1-thread-26,1,main]: 3
Thread[pool-1-thread-31,1,main]: 3
Thread[pool-1-thread-34,1,main]: 3
Thread[pool-1-thread-30,1,main]: 3
Thread[pool-1-thread-39,1,main]: 3
Thread[pool-1-thread-42,1,main]: 3
Thread[pool-1-thread-35,1,main]: 3
Thread[pool-1-thread-19,1,main]: 3
Thread[pool-1-thread-46,1,main]: 3
Thread[pool-1-thread-43,1,main]: 3
Thread[pool-1-thread-51,1,main]: 3
Thread[pool-1-thread-58,1,main]: 3
Thread[pool-1-thread-55,1,main]: 3
Thread[pool-1-thread-6,1,main]: 3
Thread[pool-1-thread-22,1,main]: 3
Thread[pool-1-thread-62,1,main]: 3
Thread[pool-1-thread-66,1,main]: 3
Thread[pool-1-thread-59,1,main]: 3
Thread[pool-1-thread-54,1,main]: 3
Thread[pool-1-thread-38,1,main]: 3
Thread[pool-1-thread-50,1,main]: 3
Thread[pool-1-thread-63,1,main]: 3
Thread[pool-1-thread-3,1,main]: 3
Thread[pool-1-thread-47,1,main]: 3
Thread[pool-1-thread-4,1,main]: 3
Thread[pool-1-thread-1,1,main]: 3
Thread[pool-1-thread-5,1,main]: 3
Thread[pool-1-thread-13,1,main]: 3
Thread[pool-1-thread-9,1,main]: 3
Thread[pool-1-thread-17,1,main]: 3
Thread[pool-1-thread-12,1,main]: 3
Thread[pool-1-thread-8,1,main]: 3
Thread[pool-1-thread-28,1,main]: 3
Thread[pool-1-thread-57,1,main]: 3
Thread[pool-1-thread-29,1,main]: 3
Thread[pool-1-thread-24,1,main]: 3
Thread[pool-1-thread-25,1,main]: 3
Thread[pool-1-thread-20,1,main]: 3
Thread[pool-1-thread-33,1,main]: 3
Thread[pool-1-thread-41,1,main]: 3
Thread[pool-1-thread-16,1,main]: 3
Thread[pool-1-thread-32,1,main]: 3
Thread[pool-1-thread-45,1,main]: 3
Thread[pool-1-thread-40,1,main]: 3
Thread[pool-1-thread-60,1,main]: 3
Thread[pool-1-thread-44,1,main]: 3
Thread[pool-1-thread-53,1,main]: 3
Thread[pool-1-thread-36,1,main]: 3
Thread[pool-1-thread-48,1,main]: 3
Thread[pool-1-thread-64,1,main]: 3
Thread[pool-1-thread-49,1,main]: 3
Thread[pool-1-thread-56,1,main]: 3
Thread[pool-1-thread-61,1,main]: 3
Thread[pool-1-thread-52,1,main]: 3
Thread[pool-1-thread-65,1,main]: 3
Thread[pool-1-thread-37,1,main]: 3
Thread[pool-1-thread-21,1,main]: 3
Thread[pool-1-thread-2,1,main]: 2
Thread[pool-1-thread-7,1,main]: 2
Thread[pool-1-thread-27,1,main]: 2
Thread[pool-1-thread-10,1,main]: 2
Thread[pool-1-thread-14,1,main]: 2
Thread[pool-1-thread-11,1,main]: 2
Thread[pool-1-thread-18,1,main]: 2
Thread[pool-1-thread-23,1,main]: 2
Thread[pool-1-thread-15,1,main]: 2
Thread[pool-1-thread-26,1,main]: 2
Thread[pool-1-thread-31,1,main]: 2
Thread[pool-1-thread-34,1,main]: 2
Thread[pool-1-thread-30,1,main]: 2
Thread[pool-1-thread-42,1,main]: 2
Thread[pool-1-thread-43,1,main]: 2
Thread[pool-1-thread-39,1,main]: 2
Thread[pool-1-thread-35,1,main]: 2
Thread[pool-1-thread-19,1,main]: 2
Thread[pool-1-thread-46,1,main]: 2
Thread[pool-1-thread-58,1,main]: 2
Thread[pool-1-thread-51,1,main]: 2
Thread[pool-1-thread-55,1,main]: 2
Thread[pool-1-thread-6,1,main]: 2
Thread[pool-1-thread-62,1,main]: 2
Thread[pool-1-thread-22,1,main]: 2
Thread[pool-1-thread-66,1,main]: 2
Thread[pool-1-thread-59,1,main]: 2
Thread[pool-1-thread-54,1,main]: 2
Thread[pool-1-thread-38,1,main]: 2
Thread[pool-1-thread-50,1,main]: 2
Thread[pool-1-thread-63,1,main]: 2
Thread[pool-1-thread-3,1,main]: 2
Thread[pool-1-thread-47,1,main]: 2
Thread[pool-1-thread-4,1,main]: 2
Thread[pool-1-thread-1,1,main]: 2
Thread[pool-1-thread-5,1,main]: 2
Thread[pool-1-thread-13,1,main]: 2
Thread[pool-1-thread-17,1,main]: 2
Thread[pool-1-thread-12,1,main]: 2
Thread[pool-1-thread-9,1,main]: 2
Thread[pool-1-thread-8,1,main]: 2
Thread[pool-1-thread-57,1,main]: 2
Thread[pool-1-thread-28,1,main]: 2
Thread[pool-1-thread-20,1,main]: 2
Thread[pool-1-thread-25,1,main]: 2
Thread[pool-1-thread-24,1,main]: 2
Thread[pool-1-thread-29,1,main]: 2
Thread[pool-1-thread-33,1,main]: 2
Thread[pool-1-thread-41,1,main]: 2
Thread[pool-1-thread-16,1,main]: 2
Thread[pool-1-thread-32,1,main]: 2
Thread[pool-1-thread-45,1,main]: 2
Thread[pool-1-thread-40,1,main]: 2
Thread[pool-1-thread-60,1,main]: 2
Thread[pool-1-thread-64,1,main]: 2
Thread[pool-1-thread-36,1,main]: 2
Thread[pool-1-thread-61,1,main]: 2
Thread[pool-1-thread-53,1,main]: 2
Thread[pool-1-thread-49,1,main]: 2
Thread[pool-1-thread-56,1,main]: 2
Thread[pool-1-thread-44,1,main]: 2
Thread[pool-1-thread-37,1,main]: 2
Thread[pool-1-thread-52,1,main]: 2
Thread[pool-1-thread-48,1,main]: 2
Thread[pool-1-thread-65,1,main]: 2
Thread[pool-1-thread-21,1,main]: 2
Thread[pool-1-thread-2,1,main]: 1
Thread[pool-1-thread-7,1,main]: 1
Thread[pool-1-thread-27,1,main]: 1
Thread[pool-1-thread-10,1,main]: 1
Thread[pool-1-thread-14,1,main]: 1
Thread[pool-1-thread-11,1,main]: 1
Thread[pool-1-thread-18,1,main]: 1
Thread[pool-1-thread-15,1,main]: 1
Thread[pool-1-thread-23,1,main]: 1
Thread[pool-1-thread-26,1,main]: 1
Thread[pool-1-thread-31,1,main]: 1
Thread[pool-1-thread-34,1,main]: 1
Thread[pool-1-thread-30,1,main]: 1
Thread[pool-1-thread-43,1,main]: 1
Thread[pool-1-thread-42,1,main]: 1
Thread[pool-1-thread-39,1,main]: 1
Thread[pool-1-thread-35,1,main]: 1
Thread[pool-1-thread-19,1,main]: 1
Thread[pool-1-thread-46,1,main]: 1
Thread[pool-1-thread-51,1,main]: 1
Thread[pool-1-thread-55,1,main]: 1
Thread[pool-1-thread-58,1,main]: 1
Thread[pool-1-thread-6,1,main]: 1
Thread[pool-1-thread-62,1,main]: 1
Thread[pool-1-thread-22,1,main]: 1
Thread[pool-1-thread-66,1,main]: 1
Thread[pool-1-thread-59,1,main]: 1
Thread[pool-1-thread-50,1,main]: 1
Thread[pool-1-thread-54,1,main]: 1
Thread[pool-1-thread-38,1,main]: 1
Thread[pool-1-thread-63,1,main]: 1
Thread[pool-1-thread-3,1,main]: 1
Thread[pool-1-thread-28,1,main]: 1
Thread[pool-1-thread-48,1,main]: 1
Thread[pool-1-thread-20,1,main]: 1
Thread[pool-1-thread-44,1,main]: 1
Thread[pool-1-thread-60,1,main]: 1
Thread[pool-1-thread-16,1,main]: 1
Thread[pool-1-thread-65,1,main]: 1
Thread[pool-1-thread-36,1,main]: 1
Thread[pool-1-thread-9,1,main]: 1
Thread[pool-1-thread-12,1,main]: 1
Thread[pool-1-thread-61,1,main]: 1
Thread[pool-1-thread-21,1,main]: 1
Thread[pool-1-thread-1,1,main]: 1
Thread[pool-1-thread-33,1,main]: 1
Thread[pool-1-thread-25,1,main]: 1
Thread[pool-1-thread-24,1,main]: 1
Thread[pool-1-thread-53,1,main]: 1
Thread[pool-1-thread-52,1,main]: 1
Thread[pool-1-thread-37,1,main]: 1
Thread[pool-1-thread-40,1,main]: 1
Thread[pool-1-thread-4,1,main]: 1
Thread[pool-1-thread-47,1,main]: 1
Thread[pool-1-thread-41,1,main]: 1
Thread[pool-1-thread-49,1,main]: 1
Thread[pool-1-thread-13,1,main]: 1
Thread[pool-1-thread-56,1,main]: 1
Thread[pool-1-thread-29,1,main]: 1
Thread[pool-1-thread-17,1,main]: 1
Thread[pool-1-thread-5,1,main]: 1
Thread[pool-1-thread-8,1,main]: 1
Thread[pool-1-thread-32,1,main]: 1
Thread[pool-1-thread-64,1,main]: 1
Thread[pool-1-thread-45,1,main]: 1
Thread[pool-1-thread-57,1,main]: 1





"Thinkinginjava" 59, the execution of the thread when we can set the corresponding 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.