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

创建线程的五种写法

目录

1.继承Thread类,并重写run()方法

2.实现Runnable接口,并重写run()方法

3.使用匿名内部类,继承Thread类,重写run方法

4.使用匿名内部类,实现Runnable接口,重写run()方法

5.使用lambda表达式


1.继承Thread类,并重写run()方法

//继承Thread类,并重写run方法 class MyThread extends Thread{ public void run(){ while(true){ System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } } public class demo1 { public static void main(String[] args) throws InterruptedException { Thread t=new MyThread(); t.start(); //main线程执行的任务 while(true){ System.out.println("hello main"); Thread.sleep(1000); } } }

2.实现Runnable接口,并重写run()方法

//实现Runnable接口,并重写run()方法 class MyRunnable implements Runnable{ @Override public void run() { while(true){ System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } } public class demo2 { public static void main(String[] args) { Runnable runnable=new MyRunnable(); Thread t=new Thread(runnable); t.start(); } }

3.使用匿名内部类,继承Thread类,重写run方法

//使用匿名内部类,继承Thread类,重写run方法 public class demo3 { public static void main(String[] args) { Thread t=new Thread(){ public void run(){ while(true){ System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; t.start(); } }

4.使用匿名内部类,实现Runnable接口,重写run()方法

//使用匿名内部类,实现Runnable接口,重写run()方法 public class demo4 { public static void main(String[] args) { Runnable runnable=new Runnable() { @Override public void run() { while(true){ System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }; Thread t=new Thread(runnable); t.start(); } }

5.使用lambda表达式

//使用lambda表达式 public class demo5 { public static void main(String[] args) { Thread t=new Thread(()->{ while(true){ System.out.println("hello thread"); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); t.start(); } }

使用lambda表示式,本质上是使用“匿名函数”,其最主要的用途是用作回调函数

在java中,方法必须要依托于类来存在,以上的函数式接口()->{},在编译器背后实际上做的工作是,创建一个匿名函数式接口的子类,并且创建出对应的实例,并重写了里面的方法。

http://www.jsqmd.com/news/93573/

相关文章:

  • 2025年十大激光平直度测量仪品牌排行榜,精准度高功能丰富专 - 工业品牌热点
  • 18、Docker生态系统工具全解析
  • LobeChat日志记录功能怎么用?用于分析用户行为与需求
  • vLLM为何能将大模型吞吐提升10倍?技术细节曝光
  • Miniconda如何支持大规模Token生成任务的环境稳定性?
  • LobeChat与Next.js的完美结合:现代Web框架赋能AI前端开发
  • 艾思独家!国家高新区数据库上线
  • 北京靠谱律所测评(2025-2026):机构口碑排名与解决方案深度解析 - 苏木2025
  • 豆包AI手机遭遇全网围剿:背后藏着什么行业秘密?
  • AutoGPT能否编写Python脚本?编程能力全面评测
  • LobeChat如何处理长文本输入?上下文长度限制与优化建议
  • LobeChat移动端适配情况实测:手机浏览器体验如何?
  • 2025年X衍射仪售后哪家好?国产仪器权威推荐+优质厂家测评+品牌口碑榜 - 品牌推荐大师1
  • LobeChat能否对接Salesforce?CRM系统智能化升级
  • Windows设备停止错误代码43 怎么解决
  • 【技术干货】向量数据库选型指南:五大主流方案性能对比与最佳实践(建议收藏)
  • AutoGPT项目灰度发布策略:新版本验证流程
  • Condaerror解决方案大全:以Miniconda为核心重建环境
  • 2025年诚信的意大利进口岩板生产厂家推荐:看哪家口碑不错? - 工业品牌热点
  • 为何漫步者能稳居市场前五?深挖一个国产音频品牌的“技术长坡”
  • 阿里PAI平台使用ESA部署模型滚动更新
  • Qwen3-14B+Function Calling:打通大模型与外部API的关键路径
  • 2025年下半年AI应用架构演进:从RAG到Agent再到MCP的生态跃迁
  • 2025年小型耐火度测定仪/全自动隔热性能测定仪五大靠谱厂家 - mypinpai
  • 大模型智能体开发指南:文件系统如何优化Agent性能!
  • 网络语言系列php系列【仅供参考】:PHP 表达式 Exception::__toString
  • ChatGPT-5.2:人工智能如何走进千家万户,改变我们的每一天
  • 网络语言系列php系列【仅供参考】:PHP Exception::getMessage 和 Exception:: getLine有什么区别
  • 如何在iPhone/iPad上重设iCloud密码
  • Spring AI Alibaba 对话记忆丢失问题:Redis 缓存过期后如何恢复 AI 上下文