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

相关文章:

  • LobeChat与Next.js的完美结合:现代Web框架赋能AI前端开发
  • 豆包AI手机遭遇全网围剿:背后藏着什么行业秘密?
  • AutoGPT能否编写Python脚本?编程能力全面评测
  • 2025年X衍射仪售后哪家好?国产仪器权威推荐+优质厂家测评+品牌口碑榜 - 品牌推荐大师1
  • 2025年下半年AI应用架构演进:从RAG到Agent再到MCP的生态跃迁
  • ChatGPT-5.2:人工智能如何走进千家万户,改变我们的每一天
  • 网络语言系列php系列【仅供参考】:PHP Exception::getMessage 和 Exception:: getLine有什么区别
  • FAQ12118:关于修改底色为白色后,设置中菜单字体显示为灰色字体问题(白底黑字)
  • Hoppscotch批量编辑完全指南:从基础到精通的高效参数管理
  • 基控电箱是什么?功能、选型与应用全指南
  • 达尔文12号在哪买:权威榜单与专业选购指南 - 品牌测评家
  • 开源AI新宠LobeChat:支持多模型切换的聊天界面解决方案
  • 自动化工程:赋能产业升级的核心引擎,从原理到应用全解析
  • 5、编程中的函数、参数传递与数组应用
  • 光刻胶增感剂用4-羟基二苯基碘鎓盐
  • 光刻胶增感剂用全氟丁基磺酸盐
  • IPv6过渡技术:从双栈到自动隧道
  • 计算机毕业设计springboot舞蹈室管理系统 基于 SpringBoot 的舞蹈培训机构综合运营平台 SpringBoot 框架下的舞蹈教室智慧排课与学员服务系统
  • 企业AI落地关键一步:vLLM生产级推理部署方案
  • 构建三维多晶模型及相关操作的探索
  • Qwen3-14B支持32K长上下文,轻松应对长文档分析任务
  • 计算机毕业设计springboot流浪猫狗救助领养平台管理系统 流浪动物救助与领养一体化平台的SpringBoot实现 基于SpringBoot的萌宠公益救助及在线领养系统
  • 通过LobeChat引流精准客户,实现大模型Token持续销售
  • 济宁婚纱照测评,远潮影像核心实力解析 - charlieruizvin
  • Postman接口测试:如何导入 swagger 接口文档?
  • 从用户反馈看改进方向:LobeChat当前局限性分析
  • 20、数据可视化管理界面的设计与工具应用
  • Soft TF-IDF算法与传统TF-IDF的区别
  • AutoGPT执行模糊目标时的澄清提问机制
  • 14、Windows与UNIX脚本编程及监控工具全解析