03_线程组和线程优先级
一、线程组
每个Thread必然存在于一个ThreadGroup中,Thread不能独立于ThreadGroup存在。
1.1、main方法的线程组
package co.dianjiu.thread;
public class MyThreadGroup {
public static void main(String[] args) {
//查看main方法所在线程组和线程名
System.out.println("Main方法线程组名===>"+
Thread.currentThread().getThreadGroup().getName());
System.out.println("Main方法线程名===>"+Thread.currentThread().getName());
}
}
输出结果
Main方法线程组名===>main
Main方法线程名===>main
1.2、不指定线程组
在new Thread时没有显式指定,那么默认将父线程(当前执行new Thread的线程)线程组设置为自己的线程组。
package co.dianjiu.thread;
public class MyThreadGroup {
public static void main(String[] args) {
Thread myThread = new Thread(()->{
System.out.println("MyThread线程组名===>"+
Thread.currentThread().getThreadGroup().getName());
System.out.println("MyThread线程名===>"+Thread.currentThread().getName());
});
myThread.start();
}
}
输出结果
MyThread线程组名===>main
MyThread线程名===>Thread-0
1.3、如何指定线程组
package co.dianjiu.thread;
public class MyThreadGroup {
public static void main(String[] args) {
//指定线程组
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
Thread myThread = new Thread(threadGroup, new MyRunnableDemo());
myThread.start();
}
public static class MyRunnableDemo implements Runnable{
@Override
public void run() {
System.out.println("MyRunnableDemo线程组名===>"+
Thread.currentThread().getThreadGroup().getName());
System.out.println("MyRunnableDemo线程名===>"+
Thread.currentThread().getName());
}
}
}
输出结果
MyRunnableDemo线程组名===>MyThreadGroup
MyRunnableDemo线程名===>Thread-0
二、线程优先级
Java中线程优先级可以指定,范围是1~10,默认的线程优先级为5。线程的执行顺序由调度程序来决定,线程的优先级会在线程被调用之前设定。通常情况下,高优先级的线程有更高的几率得到执行。
2.1、main方法的优先级
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
System.out.println("Main方法线程优先级===>"+Thread.currentThread().getPriority());
}
}
输出结果
Main方法线程优先级===>5
2.2、如何设置线程的优先级
使用setPriority()设置线程的优先级
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
Thread myThread = new Thread(()->{
System.out.println("MyThread线程优先级===>"+
Thread.currentThread().getPriority());
});
myThread.setPriority(8);
myThread.start();
}
}
输出结果
MyThread线程优先级===>8
2.3、优先级高一定先执行吗
不一定,只是优先执行的几率大些,最终的执行顺序还是由操作系统决定。
package co.dianjiu.thread;
import java.util.stream.IntStream;
public class MyThreadPriority {
public static void main(String[] args) {
IntStream.range(1, 10).forEach(i -> {
Thread thread = new Thread(new MyThreadDemo());
thread.setPriority(i);
thread.start();
});
}
public static class MyThreadDemo extends Thread {
@Override
public void run() {
super.run();
System.out.println(String.format("当前执行的线程是===>%s,优先级===>%d",
Thread.currentThread().getName(),
Thread.currentThread().getPriority()));
}
}
}
输出结果
当前执行的线程是===>Thread-15,优先级===>8
当前执行的线程是===>Thread-3,优先级===>2
当前执行的线程是===>Thread-1,优先级===>1
当前执行的线程是===>Thread-9,优先级===>5
当前执行的线程是===>Thread-7,优先级===>4
当前执行的线程是===>Thread-17,优先级===>9
当前执行的线程是===>Thread-5,优先级===>3
当前执行的线程是===>Thread-11,优先级===>6
当前执行的线程是===>Thread-13,优先级===>7
2.4、如何设置线程组的优先级
使用setMaxPriority()方法
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
threadGroup.setMaxPriority(6);
System.out.println("MyThreadGroup线程组优先级===>"+threadGroup.getMaxPriority());
}
}
输出结果
MyThreadGroup线程组优先级===>6
2.5、线程和线程组优先级不一致
当某个线程的优先级大于线程组时,线程优先级会被重置为线程组的优先级
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
//线程组优先级为6
threadGroup.setMaxPriority(6);
System.out.println("MyThreadGroup线程组优先级===>"+threadGroup.getMaxPriority());
Thread thread = new Thread(threadGroup, new MyThreadDemo());
//线程优先级为9
thread.setPriority(9);
thread.start();
}
public static class MyThreadDemo extends Thread {
@Override
public void run() {
super.run();
System.out.println(String.format("当前执行的线程是===>%s,优先级===>%d",
Thread.currentThread().getName(),
Thread.currentThread().getPriority()));
}
}
}
输出结果
MyThreadGroup线程组优先级===>6
当前执行的线程是===>Thread-1,优先级===>6
当某个线程的优先级小于线程组时,该线程的优先级不会被重置。
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
//线程组优先级为6
threadGroup.setMaxPriority(6);
System.out.println("MyThreadGroup线程组优先级===>"+threadGroup.getMaxPriority());
Thread thread = new Thread(threadGroup, new MyThreadDemo());
//线程优先级为4
thread.setPriority(4);
thread.start();
}
public static class MyThreadDemo extends Thread {
@Override
public void run() {
super.run();
System.out.println(String.format("当前执行的线程是===>%s,优先级===>%d",
Thread.currentThread().getName(),
Thread.currentThread().getPriority()));
}
}
}
输出结果
MyThreadGroup线程组优先级===>6
当前执行的线程是===>Thread-1,优先级===>4
2.6、如何使用守护线程
守护线程的默认优先级比较低,使用setDaemon(boolean on)方法设置该线程为守护线程。
如果某线程是守护线程,那如果所有的非守护线程结束,这个守护线程也会自动结束。
package co.dianjiu.thread;
import java.util.stream.IntStream;
public class MyThreadPriority {
public static void main(String[] args) {
//默认优先级为5
System.out.println("Main方法线程优先级===>"+Thread.currentThread().getPriority());
//如何设置线程优先级
Thread myThread = new Thread(()->{
System.out.println("MyThread线程优先级===>"+
Thread.currentThread().getPriority());
});
myThread.setPriority(8);
myThread.start();
//设置守护线程
Thread threadDemo = new Thread(()->{
System.out.println("MyThread守护线程优先级===>"+
Thread.currentThread().getPriority());
});
threadDemo.setDaemon(true);
threadDemo.start();
}
}
输出结果
Main方法线程优先级===>5
MyThread线程优先级===>8
MyThread守护线程优先级===>5