在操做系统中,线程能够划分优先级,优先级较高的线程获得的CPU资源越多,也就是CPI优先执行优先级较高的线程对象中的任务.dom
设置线程优先级有助于帮线程规划器肯定在下一次选择哪个线程来优先执行.ide
设置线程的优先级使用setPriority()方法.测试
在JAVA中,线程的优先级分为1~10这10个等级,若是小于1或者大于10,JDK会抛出异常 throw new IllegalArgumentException().this
在JDK中使用3个常量来预置定义优先级的值:操作系统
public final static int MIN_PRIORITY = 1;线程
public final static int NORM_PRIORITY = 5;对象
public final static int MAX_PRIORITY = 10;继承
一 . 线程优先级的继承特性进程
在JAVA中,线程的优先级具备继承性, 好比A线程启动B线程, 则 B线程的优先级与A线程是同样的资源
建立两个线程:
public class SecondThread extends Thread{ @Override public void run(){ System.out.println("Second Thread priority is : " + this.getPriority()); } }
public class FirstThread extends Thread{ @Override public void run(){ System.out.println("The First Thread priority is : " + this.getPriority()); SecondThread secondThread = new SecondThread(); secondThread.start(); } }
测试调用:
public class Test { public static void main(String[] args) { System.out.println("the current Thread priority is : " + Thread.currentThread().getPriority()); Thread.currentThread().setPriority(7); System.out.println("the current Thread priority is : " + Thread.currentThread().getPriority()); FirstThread firstThread = new FirstThread(); firstThread.start(); } }
运行结果:
当把Thread.currentThread().setPriority(7);注释后的运行结果:
二 . 优先级具备规则性
建立高低优先级的线程各一个:
public class HighLever extends Thread { @Override public void run(){ long startTime = System.currentTimeMillis(); long result = 0; Random random = new Random(); result = random.nextInt() + 1; long endTime = System.currentTimeMillis(); System.out.println("high lever thread use time :" + (endTime - startTime)); } }
public class LowLever extends Thread { @Override public void run(){ long startTime = System.currentTimeMillis(); long result = 0; Random random = new Random(); result = random.nextInt() + 1; long endTime = System.currentTimeMillis(); System.out.println("LOW lever thread use time :" + (endTime - startTime)); } }
运行结果:
优先级高的老是比优先级低的先运行完,可是不是说优先级高的先所有执行完,而且线程的优先级与代码的执行顺序无关,即线程的优先级有必定的规则性,CPU尽可能将执行资源让给优先级较高的线程执行.
三 . 优先级具备随机性
将上述代码中的优先级设置为相近的
public class Test { public static void main(String[] args) { for(int i = 0 ; i < 10 ; i ++){ HighLever highLever = new HighLever(); highLever.setPriority(2); highLever.start(); LowLever lowLever = new LowLever(); lowLever.setPriority(1); lowLever.start(); } } }
运行结果:
由运行结果能够了解,优先级高的线程不必定每次都先执行完run()方法中的任务,即线程优先级与执行顺序无关,他们的关系具备随机性.
四 . 守护线程
在JAVA中有两种线程,用户线程和守护线程.
守护线程是一种特殊的线程,当进程中不存在用户线程时,守护线程自动销毁,典型的守护线程就是垃圾回收线程,当进程中没有用户线程了,则垃圾回收线程也就没有存在的必要了,自动销毁.
守护线程最典型的应用就是垃圾回收(GC).
建立线程:
public class GuardThread extends Thread { @Override public void run(){ int i = 0 ; try{ while(true){ System.out.println("i = " + i++); Thread.sleep(2000); } }catch (Exception e){ e.printStackTrace(); } } }
测试:
public class Test { public static void main(String[] args) { try { GuardThread guardThread = new GuardThread(); guardThread.setDaemon(true); //设置守护线程 guardThread.start(); Thread.sleep(10000); System.out.println("END..........."); }catch (Exception e){ e.printStackTrace(); } } }
运行结果: