1. Thread(String name) 给线程起名字~
//给线程起名字
public class Demo03 {
public static void main(String[] args) {
Thread t = new Thread(()->{
System.out.println("子线程的名字是:"+Thread.currentThread().getName());
},"Thread线程");
t.start();
}
}
子线程的名字是:Thread线程
Process finished with exit code 0
2. getId() 线程的唯一标识,不同线程不会重复~
//线程ID
public class Demo04 {
public static void main(String[] args) {
Thread t1 = new Thread(()->{
System.out.println(Thread.currentThread().getName()+"的ID是:"+Thread.currentThread().getId());
},"线程1");
t1.start();
Thread t2 = new Thread(()->{
System.out.println(Thread.currentThread().getName()+"的ID是:"+Thread.currentThread().getId());
},"线程2");
t2.start();
}
}
线程1的ID是:16
线程2的ID是:17
Process finished with exit code 0
3. isDaemon() 是否是后台线程~
后台线程指在程序运行的时候在后台提供一种通用服务的线程,并且这种线程并不属于程序中不可或缺的部分~
JVM会在一个进程的所有非后台线程结束后,才会结束运行~
//isDaemon 是否是后台线程
public class Demo05 {
public static void main(String[] args) {
Thread t = new Thread(()->{
System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().isDaemon()+" 后台线程");
});
t.start();
}
}
Thread-0 false 后台线程
Process finished with exit code 0
4. isAlive() 是否存活
可以简单理解为 run()方法是否运行结束
//isAlive 是否存活,run方法是否运行结束
public class Demo06 {
public static void main(String[] args) {
Thread t = new Thread(()->{
System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().isAlive());
});
t.start();
}
}
Thread-0 true
Process finished with exit code 0
5. 中断线程
5.1 自定义标志位来中断线程
//使用标志位来中断线程
public class Demo07 {
private static boolean isQuit = false;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
while(!isQuit){
System.out.println("运行子线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
Thread.sleep(5000);
isQuit = true;
System.out.println("终止子线程");
}
}
运行子线程
运行子线程
运行子线程
运行子线程
运行子线程
终止子线程
Process finished with exit code 0
5.2 使用Thread.currentThread().isInterrupted() 来定义标志位
//使用Thread.currentThread().isInterrupted() 来定义标志位
public class Demo08 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
while(!Thread.currentThread().isInterrupted()){
System.out.println("运行子线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;//当触发异常后,立即退出循环
}
}
});
t.start();
Thread.sleep(5000);
t.interrupt();//t线程被中断
//如果t线程处于就绪状态,则线程被中断
//如果线程处于阻塞状态,则会抛出异常
}
}
运行子线程
运行子线程
运行子线程
运行子线程
运行子线程
java.lang.InterruptedException: sleep interrupted
at java.base/java.lang.Thread.sleep(Native Method)
at Demo0616.Demo08.lambda$main$0(Demo08.java:9)
at java.base/java.lang.Thread.run(Thread.java:831)
Process finished with exit code 0
6. join()线程等待--插队
//线程等待(插队)
public class Demo09 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
for (int i = 0; i < 5; i++) {
System.out.println("子线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
t.join(10000)//让t在10秒内先执行
for (int i = 0; i < 5; i++) {
System.out.println("主线程");
Thread.sleep(1000);
}
}
}
子线程
子线程
子线程
子线程
子线程
主线程
主线程
主线程
主线程
主线程
Process finished with exit code 0



