一牍蜂、線程組
每個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());
}
}
輸出結(jié)果
Main方法線程組名===>main
Main方法線程名===>main
1.2碑韵、不指定線程組
在new Thread時沒有顯式指定,那么默認(rèn)將父線程(當(dāng)前執(zhí)行new Thread的線程)線程組設(shè)置為自己的線程組缎脾。
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();
}
}
輸出結(jié)果
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());
}
}
}
輸出結(jié)果
MyRunnableDemo線程組名===>MyThreadGroup
MyRunnableDemo線程名===>Thread-0
二、線程優(yōu)先級
Java中線程優(yōu)先級可以指定遗菠,范圍是1~10联喘,默認(rèn)的線程優(yōu)先級為5华蜒。線程的執(zhí)行順序由調(diào)度程序來決定,線程的優(yōu)先級會在線程被調(diào)用之前設(shè)定豁遭。通常情況下叭喜,高優(yōu)先級的線程有更高的幾率得到執(zhí)行。
2.1蓖谢、main方法的優(yōu)先級
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
System.out.println("Main方法線程優(yōu)先級===>"+Thread.currentThread().getPriority());
}
}
輸出結(jié)果
Main方法線程優(yōu)先級===>5
2.2捂蕴、如何設(shè)置線程的優(yōu)先級
使用setPriority()設(shè)置線程的優(yōu)先級
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
Thread myThread = new Thread(()->{
System.out.println("MyThread線程優(yōu)先級===>"+
Thread.currentThread().getPriority());
});
myThread.setPriority(8);
myThread.start();
}
}
輸出結(jié)果
MyThread線程優(yōu)先級===>8
2.3、優(yōu)先級高一定先執(zhí)行嗎
不一定闪幽,只是優(yōu)先執(zhí)行的幾率大些啥辨,最終的執(zhí)行順序還是由操作系統(tǒng)決定。
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("當(dāng)前執(zhí)行的線程是===>%s盯腌,優(yōu)先級===>%d",
Thread.currentThread().getName(),
Thread.currentThread().getPriority()));
}
}
}
輸出結(jié)果
當(dāng)前執(zhí)行的線程是===>Thread-15溉知,優(yōu)先級===>8
當(dāng)前執(zhí)行的線程是===>Thread-3,優(yōu)先級===>2
當(dāng)前執(zhí)行的線程是===>Thread-1腊嗡,優(yōu)先級===>1
當(dāng)前執(zhí)行的線程是===>Thread-9着倾,優(yōu)先級===>5
當(dāng)前執(zhí)行的線程是===>Thread-7拾酝,優(yōu)先級===>4
當(dāng)前執(zhí)行的線程是===>Thread-17燕少,優(yōu)先級===>9
當(dāng)前執(zhí)行的線程是===>Thread-5,優(yōu)先級===>3
當(dāng)前執(zhí)行的線程是===>Thread-11蒿囤,優(yōu)先級===>6
當(dāng)前執(zhí)行的線程是===>Thread-13客们,優(yōu)先級===>7
2.4、如何設(shè)置線程組的優(yōu)先級
使用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線程組優(yōu)先級===>"+threadGroup.getMaxPriority());
}
}
輸出結(jié)果
MyThreadGroup線程組優(yōu)先級===>6
2.5材诽、線程和線程組優(yōu)先級不一致
當(dāng)某個線程的優(yōu)先級大于線程組時底挫,線程優(yōu)先級會被重置為線程組的優(yōu)先級
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
//線程組優(yōu)先級為6
threadGroup.setMaxPriority(6);
System.out.println("MyThreadGroup線程組優(yōu)先級===>"+threadGroup.getMaxPriority());
Thread thread = new Thread(threadGroup, new MyThreadDemo());
//線程優(yōu)先級為9
thread.setPriority(9);
thread.start();
}
public static class MyThreadDemo extends Thread {
@Override
public void run() {
super.run();
System.out.println(String.format("當(dāng)前執(zhí)行的線程是===>%s,優(yōu)先級===>%d",
Thread.currentThread().getName(),
Thread.currentThread().getPriority()));
}
}
}
輸出結(jié)果
MyThreadGroup線程組優(yōu)先級===>6
當(dāng)前執(zhí)行的線程是===>Thread-1脸侥,優(yōu)先級===>6
當(dāng)某個線程的優(yōu)先級小于線程組時建邓,該線程的優(yōu)先級不會被重置。
package co.dianjiu.thread;
public class MyThreadPriority {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("MyThreadGroup");
//線程組優(yōu)先級為6
threadGroup.setMaxPriority(6);
System.out.println("MyThreadGroup線程組優(yōu)先級===>"+threadGroup.getMaxPriority());
Thread thread = new Thread(threadGroup, new MyThreadDemo());
//線程優(yōu)先級為4
thread.setPriority(4);
thread.start();
}
public static class MyThreadDemo extends Thread {
@Override
public void run() {
super.run();
System.out.println(String.format("當(dāng)前執(zhí)行的線程是===>%s睁枕,優(yōu)先級===>%d",
Thread.currentThread().getName(),
Thread.currentThread().getPriority()));
}
}
}
輸出結(jié)果
MyThreadGroup線程組優(yōu)先級===>6
當(dāng)前執(zhí)行的線程是===>Thread-1官边,優(yōu)先級===>4
2.6、如何使用守護(hù)線程
守護(hù)線程的默認(rèn)優(yōu)先級比較低外遇,使用setDaemon(boolean on)方法設(shè)置該線程為守護(hù)線程注簿。
如果某線程是守護(hù)線程,那如果所有的非守護(hù)線程結(jié)束跳仿,這個守護(hù)線程也會自動結(jié)束诡渴。
package co.dianjiu.thread;
import java.util.stream.IntStream;
public class MyThreadPriority {
public static void main(String[] args) {
//默認(rèn)優(yōu)先級為5
System.out.println("Main方法線程優(yōu)先級===>"+Thread.currentThread().getPriority());
//如何設(shè)置線程優(yōu)先級
Thread myThread = new Thread(()->{
System.out.println("MyThread線程優(yōu)先級===>"+
Thread.currentThread().getPriority());
});
myThread.setPriority(8);
myThread.start();
//設(shè)置守護(hù)線程
Thread threadDemo = new Thread(()->{
System.out.println("MyThread守護(hù)線程優(yōu)先級===>"+
Thread.currentThread().getPriority());
});
threadDemo.setDaemon(true);
threadDemo.start();
}
}
輸出結(jié)果
Main方法線程優(yōu)先級===>5
MyThread線程優(yōu)先級===>8
MyThread守護(hù)線程優(yōu)先級===>5