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)