测试
package com.cky.prioritydemo; import java.util.Random; /** * Created by edison on 2017/12/3. */ public class MyThread1 extends Thread{ @Override public void run() { super.run(); long begin = System.currentTimeMillis(); long addResult =0 ; for (int j = 0; j <10 ; j++) { for (int i = 0; i <50000 ; i++) { Random random = new Random(); random.nextInt(); addResult = addResult + 1; } } long end = System.currentTimeMillis(); System.out.println("thread 1 use time="+(end-begin)); } }
1 package com.cky.prioritydemo; 2 3 import java.util.Random; 4 5 /** 6 * Created by edison on 2017/12/3. 7 */ 8 public class MyThread2 extends Thread{ 9 @Override 10 public void run() { 11 super.run(); 12 long begin = System.currentTimeMillis(); 13 long addResult =0 ; 14 for (int j = 0; j <10 ; j++) { 15 for (int i = 0; i <50000 ; i++) { 16 Random random = new Random(); 17 random.nextInt(); 18 addResult = addResult + 1; 19 } 20 } 21 22 long end = System.currentTimeMillis(); 23 System.out.println("thread 2 use time="+(end-begin)); 24 } 25 }
package com.cky.prioritydemo; /** * Created by edison on 2017/12/3. */ public class PriorityTest { public static void main(String[] args) { for (int i = 0; i < 5; i++) { MyThread1 th1 = new MyThread1(); th1.setPriority(10); th1.start(); MyThread2 th2 = new MyThread2(); th2.setPriority(1); th2.start(); } } }
C:\itsoft\jdk\bin\java -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.prioritydemo.PriorityTest thread 1 use time=344 thread 1 use time=438 thread 1 use time=438 thread 1 use time=453 thread 1 use time=485 thread 2 use time=516 thread 2 use time=516 thread 2 use time=547 thread 2 use time=547 thread 2 use time=547 Process finished with exit code 0
高优先级的线程总是大部分先执行完,但不代表高优先级的线程全部先执行完,而且当线程的优先级等级差距很大时,谁先执行完和代码调用顺序无关,测试如下
1 package com.cky.prioritydemo; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class PriorityTest { 7 public static void main(String[] args) { 8 for (int i = 0; i < 5; i++) { 9 MyThread1 th1 = new MyThread1(); 10 th1.setPriority(1); 11 th1.start(); 12 MyThread2 th2 = new MyThread2(); 13 th2.setPriority(10); 14 th2.start(); 15 } 16 } 17 }
C:\itsoft\jdk\bin\java -Didea.launcher.port=7541 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.prioritydemo.PriorityTest
thread 2 use time=297
thread 2 use time=313
thread 2 use time=328
thread 1 use time=438
thread 2 use time=485
thread 1 use time=485
thread 2 use time=500
thread 1 use time=500
thread 1 use time=516
thread 1 use time=516
Process finished with exit code 0
结果分析:大部分挺好read先执行完,说明线程的优先级具有一定的规则性,也就是cpu进行执行优先级高得线程