线程优先级、线程分类(用户线程、守护线程)
线程优先级
java 中的线程优先级的范围是1~10,默认的优先级是5。“高优先级线程”会优先于“低优先级线程”执行。
贴一段setPriority(int newPriority)源码:
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);
}
}
源码看出,线程优先级范围是0~10,默认是5
获得 线程的优先级:
public final int getPriority() {
return priority;
}
线程优先级demo:
public class TestMyThread {
public static void main(String[] args) {
MyThread mt1 = new MyThread();
MyThread mt2 = new MyThread();
MyThread mt = new MyThread();
mt1.setPriority(1);// 设置线程的优先级为1
mt2.setPriority(10); // 设置线程的优先级10
mt.start(); // 默认情况下线程的优先级为5
mt1.start();
mt2.start();
}
}
class MyThread extends Thread {
@Override
public void run() {
long begin = System.currentTimeMillis();
for (int i = 0; i < 50000000; i++) {
int a=2000*20;
}
long end = System.currentTimeMillis();
long time = end - begin;
System.out.println(Thread.currentThread().getName() + "优先级为:" + this.getPriority() + " 执行完毕,花费时间为" + time); // getPriority()方法可以获取线程的优先级
}
}
运行5次的结果:
Thread-1优先级为:10 执行完毕,花费时间为3
Thread-2优先级为:5 执行完毕,花费时间为28
Thread-0优先级为:1 执行完毕,花费时间为29Thread-1优先级为:10 执行完毕,花费时间为2
Thread-0优先级为:1 执行完毕,花费时间为31
Thread-2优先级为:5 执行完毕,花费时间为32Thread-1优先级为:10 执行完毕,花费时间为3
Thread-2优先级为:5 执行完毕,花费时间为24
Thread-0优先级为:1 执行完毕,花费时间为24Thread-2优先级为:5 执行完毕,花费时间为3
Thread-0优先级为:1 执行完毕,花费时间为22
Thread-1优先级为:10 执行完毕,花费时间为22Thread-2优先级为:5 执行完毕,花费时间为3
Thread-1优先级为:10 执行完毕,花费时间为25
Thread-0优先级为:1 执行完毕,花费时间为25
结果可以看出:并不是优先级高的线程先于优先级低的线程执行完毕,而是线程的优先级只能确保CPU尽量将执行的资源让给优先级高的线程用,但不保证定义的高优先级的线程的大部分都能先于低优先级的线程执行完,所以不建议设置线程的优先级。
线程分类
线程分类:
用户线程和守护线程
用户线程和守护线程的区别
二者其实基本上是一样的。唯一的区别在于JVM何时离开。
用户线程:当存在任何一个用户线程未离开,JVM是不会离开的。
守护线程:如果只剩下守护线程未离开,JVM是可以离开的。
在Java中,制作守护线程非常简单,直接利用.setDaemon(true)
用户线程
定义:平时用到的普通线程均是用户线程,当在Java程序中创建一个线程,它就被称为用户线程
守护线程
定义:是个服务线程,准确地来说就是服务其他的线程,这是它的作用——而其他的线程只有一种,那就是用户线程。所以java里线程分2种,
1、守护线程,比如垃圾回收线程,就是最典型的守护线程。
2、用户线程,就是应用程序里的自定义线程
守护线程demo:
class MyThread extends Thread{
public MyThread(String name) {
super(name);
}
public void run(){
try {
for (int i=0; i<5; i++) {
Thread.sleep(3);
System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);
}
} catch (InterruptedException e) {
}
}
};
class MyDaemon extends Thread{
public MyDaemon(String name) {
super(name);
}
public void run(){
try {
for (int i=0; i<10000; i++) {
Thread.sleep(1);
System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);
}
} catch (InterruptedException e) {
}
}
}
public class Demo {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()
+"(isDaemon="+Thread.currentThread().isDaemon()+ ")");
Thread t1=new MyThread("t1"); // 新建t1
Thread t2=new MyDaemon("t2"); // 新建t2
t2.setDaemon(true); // 设置t2为守护线程
t1.start(); // 启动t1
t2.start(); // 启动t2
}
}
运行结果:
main(isDaemon=false)
t2(isDaemon=true), loop 0
t2(isDaemon=true), loop 1
t1(isDaemon=false), loop 0
t2(isDaemon=true), loop 2
t2(isDaemon=true), loop 3
t1(isDaemon=false), loop 1
t2(isDaemon=true), loop 4
t2(isDaemon=true), loop 5
t2(isDaemon=true), loop 6
t1(isDaemon=false), loop 2
t2(isDaemon=true), loop 7
t2(isDaemon=true), loop 8
t2(isDaemon=true), loop 9
t1(isDaemon=false), loop 3
t2(isDaemon=true), loop 10
t2(isDaemon=true), loop 11
t1(isDaemon=false), loop 4
t2(isDaemon=true), loop 12
结果说明:
(01) 主线程main是用户线程,它创建的子线程t1也是用户线程。
(02) t2是守护线程。在“主线程main”和“子线程t1”(它们都是用户线程)执行完毕,只剩t2这个守护线程的时候,JVM自动退出。
还没有评论,来说两句吧...