jdk中有3个常量来定义优先级
public final static int MIN_PRIOPITY = 1;
public final static int NORM_PRIOPITY = 5;
public final static int MAX_PRIOPITY = 10;
在java中,线程的优先级具有继承性,比如A线程启动B线程,则B线程的优先级和A时一样的
测试
1 package com.cky.demo; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class MyThread1 extends Thread{ 7 @Override 8 public void run() { 9 super.run(); 10 System.out.println("线程1:"+ this.getPriority()); 11 Thread2 thread2 = new Thread2(); 12 thread2.start(); 13 } 14 }
1 package com.cky.demo; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class Thread2 extends Thread { 7 @Override 8 public void run() { 9 System.out.println("线程2:"+ this.getPriority()); 10 } 11 }
1 package com.cky.demo; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class TestDemo { 7 public static void main(String[] args) { 8 System.out.println("main " + Thread.currentThread().getPriority()); 9 //Thread.currentThread().setPriority(6); 10 System.out.println("main " + Thread.currentThread().getPriority()); 11 MyThread1 th = new MyThread1(); 12 th.start(); 13 } 14 }
C:itsoftjdkinjava -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib esources.jar;C:itsoftjdkjrelib t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.demo.TestDemo main 5 main 5 线程1:5 线程2:5
当去掉注释
package com.cky.demo; /** * Created by edison on 2017/12/3. */ public class TestDemo { public static void main(String[] args) { System.out.println("main " + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(6); System.out.println("main " + Thread.currentThread().getPriority()); MyThread1 th = new MyThread1(); th.start(); } }
main 5 main 6 线程1:6 线程2:6