当前位置: 首页 > news >正文

Thread类及常见方法

一、Thread的常见构造方法

方法说明
Thread()

这会创建一个默认的线程对象,没有指定要执行的任务

Thread(Runnable target)

通过传入一个 Runnable 接口的实现类来指定线程要执行的任务

Thread(String name)

创建线程并指定线程名称,便于调试和日志记录

Thread(Runnable target,String name)

同时指定任务和名称

示例:

// 无参构造 Thread thread1 = new Thread(); // 带 Runnable Runnable task = () -> System.out.println("Hello from thread"); Thread thread2 = new Thread(task); // 带名称 Thread thread3 = new Thread("MyThread"); // 带 Runnable 和名称 Thread thread4 = new Thread(task, "MyTaskThread");

二、Thread的几种常见属性

属性获取方法
线程名称getName()
线程优先级getPriority()
守护线程标志isDeamo()
线程IDgetId()
线程状态getState()

示例:

Thread thread = new Thread(); System.out.println("Name: " + thread.getName()); // Thread-0 System.out.println("Priority: " + thread.getPriority()); // 5 System.out.println("Is Daemon: " + thread.isDaemon()); // false System.out.println("ID: " + thread.getId()); // 例如 12 System.out.println("State: " + thread.getState()); // NEW

三、启动一个线程-start()

要启动一个线程,不能直接调用 run() 方法(那样只是普通方法调用,不会开启新线程),而是要调用 start() 方法。start() 会通知 JVM 创建一个新线程,并调用 run() 方法。

  • public void start():启动线程。一旦调用,线程进入 RUNNABLE 状态。
  • 注意:一个线程只能调用一次 start(),多次调用会抛出 IllegalThreadStateException。

示例:

Thread thread = new Thread(() -> { System.out.println("Thread is running"); }); thread.start(); // 启动线程

四、中断一个线程

方法说明

public void interrupt()

向线程发送中断信号,设置中断标志

public boolean isInterrupted()

检查中断标志(不清除)

public static boolean interrupted()

检查并清除中断标志

示例:

Thread thread = new Thread(() -> { while (!Thread.interrupted()) { System.out.println("Working..."); } System.out.println("Interrupted!"); }); thread.start(); // 稍后中断 thread.interrupt();

五、等待一个线程-join()

方法说明

public void join()

无限等待,直到目标线程结束

public void join(long millis)

等待指定毫秒,如果超时则继续

public void join(long millis, int nanos)

更精确的超时等待

示例:

Thread thread = new Thread(() -> { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread finished"); }); thread.start(); try { thread.join(); // 主线程等待 thread 完成 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Main thread continues");

六、获取当前线程引用

方法说明

public static Thread currentThread()

返回当前线程的 Thread 对象

示例:

public class Main { public static void main(String[] args) { Thread current = Thread.currentThread(); System.out.println("Current thread: " + current.getName()); // main new Thread(() -> { Thread inner = Thread.currentThread(); System.out.println("Inner thread: " + inner.getName()); // Thread-0 }).start(); } }

七、休眠当前线程

方法说明

public static void sleep(long millis)

休眠指定毫秒

public static void sleep(long millis, int nanos)

更精确的休眠

示例:

try { Thread.sleep(1000); // 休眠 1 秒 } catch (InterruptedException e) { // 处理中断 Thread.currentThread().interrupt(); // 重新设置中断标志 } System.out.println("Woke up!");
http://www.jsqmd.com/news/86236/

相关文章:

  • CubeFS数据备份与恢复:构建企业级业务连续性的完整指南
  • WAN2.2-14B-Rapid-AllInOne:重新定义AI视频生成效率的革命性框架
  • 彻底告别BlackHole残留:3步完美清理macOS音频驱动
  • 轻松上手!Zen Browser多语言设置完全指南:让你的浏览器说你的语言
  • 5个BongoCat窗口管理技巧:让你的桌面伙伴更懂你
  • 如何快速掌握Java虚拟机:面向开发者的终极学习指南
  • 基于vue的招聘求职人才库平台_d852tptg_springboot php python nodejs
  • Scrypted:重新定义智能家居视频管理的全能解决方案
  • NetSonar网络诊断工具:跨平台网络问题终极解决方案
  • 快手开源KAT-V1-40B:终结AI“过度思考“,动态推理技术节省60%算力成本
  • unopim开源PIM系统:重塑企业产品数据管理的智能化解决方案
  • 专业的全自动睡眠呼吸机品牌排行榜一览 - 品牌排行榜
  • 基于vue的智慧仓库预警管理系统设计与实现_0m8200p8_springboot php python nodejs
  • 数字漫画收藏的终极方案:Mangadex下载器完整使用手册
  • 如何用LOOT终极优化游戏模组:新手完整避坑指南
  • 好用的单水平呼吸机品牌哪家好?国内优质品牌推荐 - 品牌排行榜
  • 视频去水印黑科技:告别遮挡,还原纯净画面
  • 好用的家用呼吸机品牌哪家好?五大品牌推荐 - 品牌排行榜
  • PyTorch-Meta元学习数据集完整教程:从选择到实战
  • Nunchaku FLUX.1-Krea-dev量化模型:轻量化AI图像生成新标准
  • 好用的双水平呼吸机品牌哪家好?这几款值得关注 - 品牌排行榜
  • Metabase数据建模实战:从问题诊断到可视化分析的完整指南
  • 3D重建质量评估终极指南:从零到一的完整解决方案
  • 上海靠谱的劳务外包品牌推荐排行榜单 - 品牌排行榜
  • 8、JDBC-连接池Druid
  • AI智能体测试终极指南:从崩溃频发到稳定运行
  • 19、Unix系统中的进程通信机制详解
  • Voxtral Mini 3B:小模型撬动大变革,多模态语音交互新纪元
  • 上海知名的猎头招聘公司口碑排行一览 - 品牌排行榜
  • 基于vue的智慧旅游网站设计与实现_b79447q7_springboot php python nodejs