当前位置: 首页 > 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.rkmt.cn/news/96471.html

相关文章:

  • 轻松上手!Zen Browser多语言设置完全指南:让你的浏览器说你的语言
  • 5个BongoCat窗口管理技巧:让你的桌面伙伴更懂你
  • 基于vue的招聘求职人才库平台_d852tptg_springboot php python nodejs
  • 快手开源KAT-V1-40B:终结AI“过度思考“,动态推理技术节省60%算力成本
  • unopim开源PIM系统:重塑企业产品数据管理的智能化解决方案
  • 专业的全自动睡眠呼吸机品牌排行榜一览 - 品牌排行榜
  • 好用的单水平呼吸机品牌哪家好?国内优质品牌推荐 - 品牌排行榜
  • 好用的家用呼吸机品牌哪家好?五大品牌推荐 - 品牌排行榜
  • PyTorch-Meta元学习数据集完整教程:从选择到实战
  • Nunchaku FLUX.1-Krea-dev量化模型:轻量化AI图像生成新标准
  • 好用的双水平呼吸机品牌哪家好?这几款值得关注 - 品牌排行榜
  • 上海靠谱的劳务外包品牌推荐排行榜单 - 品牌排行榜
  • 8、JDBC-连接池Druid
  • 19、Unix系统中的进程通信机制详解
  • 上海知名的猎头招聘公司口碑排行一览 - 品牌排行榜
  • 基于vue的智慧旅游网站设计与实现_b79447q7_springboot php python nodejs
  • 21、Linux系统启动与模块管理详解
  • MATLAB 2008B终极安装指南:从零开始快速配置
  • 5分钟掌握ECharts高级筛选:打造极致交互体验的数据可视化
  • 22、深入解析Linux内核:结构、功能与资源指南
  • 5大核心功能解析:vxe-table如何成为Vue表格开发的终极解决方案?
  • 终极AI诗词创作平台:CM_Poem_Master完全指南
  • 5个MPV播放器窗口优化技巧,让观影体验提升200%
  • Qwen2.5-32B-DialogueReason:智能对话推理新范式,重新定义行业交互体验
  • MidiStroke:解锁音乐创作新维度的MIDI控制器映射神器
  • Qwen3-Reranker-8B:阿里多语言重排模型重构企业检索系统
  • Rufus完全攻略:轻松制作专业级USB启动盘
  • Hy区块链开发:Lisp智能合约的技术革命与实战指南
  • 敏捷性是制定强大生成式AI战略的关键,亚马逊云科技如何做到?
  • OpenPose深度配置指南:7大核心模块解析与性能优化实战