news 2026/10/23 6:39:40

Thread 类的基本用法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 线程结束,程序退出

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/23 6:38:34

全文 - 第01章 - 简介 - InfiniBand Specification release 1.4

「译注&#xff1a;本章适合浏览&#xff0c;不需要精读」 本文档是 InfiniBand 架构规范的第一卷。它是描述该架构的一系列文档中的第一份。 1.1 致谢 以下人员在 InfiniBand 架构规范本卷的编写过程中做出了重要贡献&#xff1a; 指导委员会成员 联合主席&#xff1a; 成员&a…

作者头像 李华
网站建设 2026/10/23 6:38:15

【译】今天我将审核 GitHub 拉取请求

今天我将向大家演示&#xff0c;在开发团队协作或者管理开源项目时&#xff0c;如何处理最常见的工作之一&#xff1a;拉取请求&#xff08;Pull Request&#xff09;。本次演示我们会使用 Visual Studio 内置的 GitHub 工具&#xff0c;不过这套操作思路在 Azure DevOps 中也大…

作者头像 李华
网站建设 2026/10/23 6:34:32

Spring Boot购物网站源码实战:下单事务、库存扣减与防超卖

简介&#xff1a;这套购物网站项目源码与数据库面向JavaWeb初学者、课程设计及毕业设计参考者&#xff0c;提供一套功能较完整的类淘宝电商实现&#xff0c;适合用作电商项目入门拆解样本&#xff0c;可帮助读者理解从商品浏览、搜索、购物车到下单、用户管理等业务流程如何落地…

作者头像 李华
网站建设 2026/10/23 6:34:27

圆瓶贴附路径离散:弦高、首段方向与分段数校核

壹星&#xff08;YIXING&#xff09;技术笔记&#xff5c;东莞市壹星自动化设备有限公司把圆瓶贴附路径画成若干直线段时&#xff0c;第一条弦并不与起点切线重合。段数太少会同时产生径向轮廓误差和首段方向误差&#xff0c;但这属于离散表示误差&#xff0c;不能直接解释真实…

作者头像 李华
网站建设 2026/10/23 6:30:45

在现代前端开发中,UI 组件库的选择直接影响项目的开发效率、维护成本和用户体验。Angular Material 作为 Google 官方团队开发和维护的 UI 组件库

在现代前端开发中&#xff0c;UI 组件库的选择直接影响项目的开发效率、维护成本和用户体验。Angular Material 作为 Google 官方团队开发和维护的 UI 组件库&#xff0c;实现了 Google 的 Material Design 设计规范&#xff0c;为 Angular 开发者提供了一套高质量、可访问、易…

作者头像 李华