线程优先级介绍
关于线程的优先级先看一段Thread中源码的注释
Every thread has a priority. Threads with higher priority are
executed in preference to threads with lower priority. Each thread
may or may not also be marked as a daemon. When code running in
some thread creates a new <code>Thread</code> object, the new
thread has its priority initially set equal to the priority of the
creating thread, and is a daemon thread if and only if the
creating thread is a daemon.
大概意思就是以下几点
每个线程都有优先级。
线程优先级高的线程会比优先级低的先执行。
每个线程都可以被标记为守护线程。
被创建的线程的默认优先级和创建它的那个线程的优先级相同,
而且创建它的那个线程如果为守护线程则被创建的这个线程也为守护线程。
线程优先级的值
/**
* 优先级最小值
*/
public final static int MIN_PRIORITY = 1;
/**
* 优先级默认值
*/
public final static int NORM_PRIORITY = 5;
/**
* 优先级最大值
*/
public final static int MAX_PRIORITY = 10;
下面这段代码演示如何设置线程的优先级,以及验证线程优先级对执行顺序的影响:
static class MyThread1 extends Thread {
String name;
public MyThread1(String name) {
this.name = name;
}
@Override
public void run() {
super.run();
System.out.println(name+" is running"+"and priority is "+getPriority());
}
}
public static void main(String[] args) {
MyThread1 myThread = new MyThread1("myThread");
MyThread1 myThread2 = new MyThread1("myThread2");
myThread.setPriority(3);
myThread2.setPriority(8);
myThread.start();
myThread2.start();
}
打印结果:
myThread2 is runningand priority is 8
myThread is runningand priority is 3
可以看到通过setPriority方法将myThread2的优先级设置为8之后它会比myThread优先执行。
守护线程
还是来看段源码中的英文介绍
When a Java Virtual Machine starts up, there is usually a single
non-daemon thread (which typically calls the method named
<code>main</code> of some designated class). The Java Virtual
Machine continues to execute threads until either of the following
occurs:
The <code>exit</code> method of class <code>Runtime</code> has been
called and the security manager has permitted the exit operation
to take place.
All threads that are not daemon threads have died, either by
returning from the call to the <code>run</code> method or by
throwing an exception that propagates beyond the <code>run</code>
method.
大概意思是:
当JVM启动的时候通常只有一个非守护线程也叫做main线程。
如果不是下面两种情况发生,JVM会一直执行下去
一、exit方法被调用;
二、所有的非守护线程都执行完了。
先看下面一段代码:
static class MyThread extends Thread {
String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
super.run();
boolean isDaemon = this.isDaemon();
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+i+"and is daemon "+ isDaemon);
}
}
}
static class DaemonThread extends Thread{
String name;
public DaemonThread(String name) {
this.name = name;
}
@Override
public void run() {
super.run();
boolean isDaemon = this.isDaemon();
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+i+"and is daemon "+ isDaemon);
}
}
}
public static void main(String[] args) {
System.out.println("main is "+Thread.currentThread().isDaemon());
MyThread myThread = new MyThread("myThread");
DaemonThread daemonThread = new DaemonThread("daemonThread");
daemonThread.setDaemon(true);
myThread.start();
daemonThread.start();
}
打印结果:
main is false
daemonThread0and is daemon true
daemonThread1and is daemon true
myThread0and is daemon false
daemonThread2and is daemon true
daemonThread3and is daemon true
myThread1and is daemon false
daemonThread4and is daemon true
myThread2and is daemon false
daemonThread5and is daemon true
daemonThread6and is daemon true
daemonThread7and is daemon true
myThread3and is daemon false
daemonThread8and is daemon true
myThread4and is daemon false
daemonThread9and is daemon true
daemonThread10and is daemon true
myThread5and is daemon false
daemonThread11and is daemon true
daemonThread12and is daemon true
myThread6and is daemon false
daemonThread13and is daemon true
daemonThread14and is daemon true
myThread7and is daemon false
daemonThread15and is daemon true
daemonThread16and is daemon true
myThread8and is daemon false
daemonThread17and is daemon true
daemonThread18and is daemon true
myThread9and is daemon false
分析打印结果:
1、设置线程为守护线程
daemonThread.setDaemon(true);
2、查看线程是否是守护线程
isDaemon()
3、可以看到当非守护线程MyThread工作结束后由于没有其他的非工作线程,所以守护线程DaemonThread就结束了。