ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

Thread 类的基本用法

Thread 类的基本用法

一.线程创建

第一方式:创建子类继承Thread 重写run方法

package Thread_star; public class Dome1 extends Thread { @Override public void run() { while (true){ System.out.println("hello thread"); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public static void main(String[] args) throws InterruptedException { Thread thread = new Dome1(); thread.start(); while (true){ System.out.println("hello main"); thread.sleep(1_000); } } }

第二方式:继承Runnable这个接口 重写run方法

package Thread_star; public class Dome2 implements Runnable{ @Override public void run() { while (true){ System.out.println("hello thread"); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } public static void main(String[] args) throws InterruptedException { Dome2 dome2 = new Dome2(); Thread thread = new Thread(dome2); thread.start(); while (true){ System.out.println("hello main"); thread.sleep(1_000); } } }

第三方式:直接创建Thread 的子类 匿名内部类重写run方法

package Thread_star; public class Dome3 { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(){ @Override public void run() { while (true){ System.out.println("hello thread"); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; thread.start(); while (true){ System.out.println("hello main"); thread.sleep(1_000); } } }

第四方式:直接创建Runnable的子类 匿名内部类重写run方法

package Thread_star; public class Dome4 { public static void main(String[] args) throws InterruptedException { Runnable runnable = new Runnable() { @Override public void run() { while (true){ System.out.println("hello thread"); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; Thread thread = new Thread(runnable); thread.start(); while (true){ System.out.println("hello main"); thread.sleep(1_000); } } }

第五方式:直接创建Thread 的子类 Lambda表达式 重写run方法

package Thread_star; public class Dome5 { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(() -> {while (true){ System.out.println("hello thread"); try { Thread.sleep(1_000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }, "尝试1"); thread.start(); while (true){ System.out.println("hello main"); thread.sleep(1_000); } } }

二.线程中断 && 获取线程实例 &&线程等待 && 线程休眠

package Thread_star; import java.util.Scanner; public class Dome6 { public static int count = 0; public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(() -> { //线程的获取实力方式 while (!Thread.currentThread().isInterrupted()){ System.out.println("线程中断"); try { //线程的休眠 sleep方法 Thread.sleep(10_000); } catch (InterruptedException e) { break; } } },"线程中断"); //《--这个位置给线起名字 //启动线程 thread.start(); //获取线程的名字 System.out.println(thread.getName()); for (int i = 0; i < 10; i++) { count++; } System.out.println(count); Scanner scanner = new Scanner(System.in); System.out.println("输入0 中断线程"); int n = scanner.nextInt(); if (n == 0){ thread.interrupt(); } thread.join(); } }

1.线程中断

(1).方式是在线程里面用

(2)先用currentThread()方法返回当前代码所在线程的对象;

(3)再用isInterrupted()方法返回一个 boolean 值,读取线程内部的「中断标志位」;

(4)下面的interrupt()方法用于改变 isInterrupted 中的值,isInterrupted 默认值为 false。

2.线程等待

(1).join()当前线程(调用者)等待 x 线程执行完毕后,再继续往下走。

上述代码就是等待thread运行完之后再去运行maim这个主线程

3.线程休眠

(1).sleep(毫秒)让「当前线程」暂停指定时间,交出 CPU。 等完后在去运行。

4.获取线程实例

(1)Thread.currentThread()拿到「当前代码所在」的线程对象

完成的流程

【准备阶段 - main 线程】
1. new Thread(...) 创建子线程对象,起名"线程中断"(此时未启动)
2. thread.start() 启动子线程,main 与子线程开始【并发】执行

【并发阶段】
main 线程做什么:
a. thread.getName() 打印线程名 → 输出"线程中断"
b. for 循环 count++ 十次 → count = 10
c. println(count) → 输出 10
d. 创建 Scanner,打印"输入0 中断线程"
e. scanner.nextInt() → main 阻塞,等待你输入

子线程做什么:
a. 判断 !isInterrupted() 为 true → 进入循环
b. 打印"线程中断"
c. Thread.sleep(10000) → 睡着 10 秒

【中断阶段】(当你输入 0 并回车)
3. n == 0 成立 → 调用 thread.interrupt()
4. interrupt() 做两件事:
- 把子线程的中断标志位设为 true
- 唤醒正在 sleep 的子线程,抛出 InterruptedException
5. 子线程 catch 到异常 → break → 跳出循环 → 子线程结束

【等待结束阶段】
6. thread.join() → main 等子线程结束(此时子线程已退出,join 立刻通过)
7. main 线程结束,程序退出

RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进