Thread 类的基本用法

发布时间:2026/10/23 6:39:45
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.获取线程实例1Thread.currentThread()拿到「当前代码所在」的线程对象完成的流程【准备阶段 - main 线程】1. new Thread(...) 创建子线程对象起名线程中断此时未启动2. thread.start() 启动子线程main 与子线程开始【并发】执行【并发阶段】main 线程做什么a. thread.getName() 打印线程名 → 输出线程中断b. for 循环 count 十次 → count 10c. println(count) → 输出 10d. 创建 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 的子线程抛出 InterruptedException5. 子线程 catch 到异常 → break → 跳出循环 → 子线程结束【等待结束阶段】6. thread.join() → main 等子线程结束此时子线程已退出join 立刻通过7. main 线程结束程序退出

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询