线程的优先级
在操作系统中, 线程可以划分优先级, 优先级较高的会先得到CPU的资源较多, 也就是会优先执行优先级高的线程中的任务
通过设置优先级有助于帮"线程规划期"确定下一次选择哪个线程来优先执行
线程中优先级分为1~10这10个等级, 如果你设置的大于10或者小于1, 则JDK会抛出异常throw new IllegalArgumentException()
源码分析
public final static int MIN_PRIORITY = 1 ;
public final static int NORM_PRIORITY = 5 ;
public final static int MAX_PRIORITY = 10 ;
public final void setPriority ( int newPriority) {
ThreadGroup g;
checkAccess ( ) ;
if ( newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException ( ) ;
}
if ( ( g = getThreadGroup ( ) ) != null) {
if ( newPriority > g. getMaxPriority ( ) ) {
newPriority = g. getMaxPriority ( ) ;
}
setPriority0 ( priority = newPriority) ;
}
}
继承性
Java线程的优先级有继承性, 比如A线程启动B线程, 则B的优先级与A的是一样的
public class MyThread1 extends Thread {
@Override
public void run ( ) {
System. out. println ( "我是MyThread1我的优先级是:" + this . getPriority ( ) ) ;
MyThread2 thread2 = new MyThread2 ( ) ;
thread2. start ( ) ;
}
}
class MyThread2 extends Thread {
@Override
public void run ( ) {
System. out. println ( "我是MyThread1我的优先级是:" + this . getPriority ( ) ) ;
}
}
public class Run {
public static void main ( String[ ] args) {
System. out. println ( "我是main我的优先级是:" + Thread. currentThread ( ) . getPriority ( ) ) ;
Thread. currentThread ( ) . setPriority ( 9 ) ;
System. out. println ( "我是修改后的main我的优先级是:" + Thread. currentThread ( ) . getPriority ( ) ) ;
MyThread1 thread1 = new MyThread1 ( ) ;
thread1. start ( ) ;
}
}
我是main我的优先级是: 5
我是修改后的main我的优先级是: 9
我是MyThread1我的优先级是: 9
我是MyThread1我的优先级是: 9
Process finished with exit code 0
规则性
所谓规则性就是高优先级的线程在大多数情况下是优先执行完的. 但这并不代表优先级高的线程全部是优先执行完, 还会和代码的调用顺序有关, 除非有俩个线程的优先级差距很大, 这时就和代码的调用顺序无关了
例如下面代码
public class MyThread1 extends Thread {
@Override
public void run ( ) {
this . setPriority ( 1 ) ;
System. out. println ( "我是MyThread1我的优先级是:" + this . getPriority ( ) ) ;
MyThread2 thread2 = new MyThread2 ( ) ;
thread2. start ( ) ;
int i = 0 ;
while ( true ) {
i++ ;
System. out. println ( "我是MyThread1=" + i) ;
}
}
}
class MyThread2 extends Thread {
@Override
public void run ( ) {
this . setPriority ( 9 ) ;
System. out. println ( "我是MyThread1我的优先级是:" + this . getPriority ( ) ) ;
int i = 0 ;
while ( true ) {
i++ ;
System. out. println ( "我是MyThread2=" + i) ;
}
}
}
明显是1先启动, 但是由于优先级差距较大, 所以他俩的执行就和代码调用顺序无关了
是MyThread1= 71681
我是MyThread1= 71682
我是MyThread1= 71683
我是MyThread1= 71684
我是MyThread1= 71685
我是MyThread2= 241867
我是MyThread2= 241868
我是MyThread2= 241869
我是MyThread2= 241870
我是MyThread2= 241871
我是MyThread2= 241872
我是MyThread2= 241873
我是MyThread2= 241874
随机性
所谓随机性就是上面说到的如果俩个线程的优先级差距很小的话, 不一定优先级较高的那个线程每一都是先执行完
例如下面代码, 即使你比我较高, 而且你还是先启动, 但是咋俩的执行结果是差不多的
public class MyThread1 extends Thread {
@Override
public void run ( ) {
this . setPriority ( 6 ) ;
System. out. println ( "我是MyThread1我的优先级是:" + this . getPriority ( ) ) ;
MyThread2 thread2 = new MyThread2 ( ) ;
thread2. start ( ) ;
int i = 0 ;
while ( true ) {
i++ ;
System. out. println ( "我是MyThread1=" + i) ;
}
}
}
class MyThread2 extends Thread {
@Override
public void run ( ) {
this . setPriority ( 5 ) ;
System. out. println ( "我是MyThread1我的优先级是:" + this . getPriority ( ) ) ;
int i = 0 ;
while ( true ) {
i++ ;
System. out. println ( "我是MyThread2=" + i) ;
}
}
}
我是MyThread1= 163056
我是MyThread1= 163057
我是MyThread1= 163058
我是MyThread1= 163059
我是MyThread1= 163060
我是MyThread2= 143756
我是MyThread2= 143757
我是MyThread2= 143758
守护线程
在Java多线程中有俩种线程, 一种是用户线程, 一种是守护线程
守护线程是一种特殊的线程, 具有陪伴的意思, 当线程中不存在非守护线程了, 那么守护线程就会自动销毁, 最典型的守护线程就是垃圾回收器, 也可以这样说, 任何一个守护线程都是JVM中所有非守护线程的保姆
Daemon的作用就是为其他线程的运行提供便利
public class DaemonTest extends Thread {
@Override
public void run ( ) {
try {
int i = 0 ;
while ( true ) {
System. out. println ( "i=" + i++ ) ;
Thread. sleep ( 1000 ) ;
}
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
public class Run2 {
public static void main ( String[ ] args) throws InterruptedException {
DaemonTest daemonTest = new DaemonTest ( ) ;
daemonTest. setDaemon ( true ) ;
daemonTest. start ( ) ;
Thread. sleep ( 5000 ) ;
System. out. println ( "非守护线程执行完毕, 守护线程应该停止" ) ;
}
}
i= 0
i= 1
i= 2
i= 3
i= 4
非守护线程执行完毕, 守护线程应该停止
Process finished with exit code 0