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

JAVA EE初阶 2: 多线程-初阶

1.认识线程Thread1.1概念1.2创建线程1方法1 - 继承Thread类【 Ctrl C 取消 终止 】2方法2 - 实现Runnable接口3总结4匿名内部类5lambda表达式最常用2.Thread类及常见方法2.1构造方法2.2常见属性2.3启动一个线程 start()调用start方法才真的在操作系统的底层创建出一个线程。2.4中断一个线程package thread; public class Demo7 { private static boolean flag true; public static void main(String[] args) throws InterruptedException { Thread t new Thread(() - { while(flag){ System.out.println(hello thread); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t.start(); System.out.println(hello main); Thread.sleep(3000); flag false; System.out.println(让 t 线程终止); } }package thread; public class Demo7 { public static void main(String[] args) throws InterruptedException { Thread t new Thread(() - { while(Thread.currentThread().isInterrupted()){ System.out.println(hello thread); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t.start(); System.out.println(hello main); Thread.sleep(3000); t.interrupt(); System.out.println(t 线程的中断标志位 t.isInterrupted()); } }2.5等待一个线程 join()有时我们需要等待一个线程完成它的工作后才能进的下⼀步工作这时我们需要⼀个方法明确等待线程的结束。主线程等待代码package thread; public class Demo7 { public static void main(String[] args) throws InterruptedException { Thread t new Thread(() - { for(int i 0; i 5; i){ System.out.println(hello thread); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block //e.printStackTrace(); break; } } }); System.out.println(主线程开始); t.start(); System.out.println(主线程等待之前); t.join(); System.out.println(主线程等待之后); System.out.println(主线程结束); } }t线程等待代码package thread; public class Demo7 { public static void main(String[] args) throws InterruptedException { Thread mainThread Thread.currentThread(); Thread t new Thread(() - { try { System.out.println(t 线程等待之前); mainThread.join(); System.out.println(t 线程等待之后); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }); t.start(); for(int i 0; i 3; i){ System.out.println(hello main); Thread.sleep(1000); } } }2.6获取当前线程引用2.7休眠当前线程3.线程的状态线程的状态是⼀个枚举类型 Thread.State4.多线程带来的的风险-线程安全(重点)4.1概念 例子4.2线程不安全的原因5.死锁重点Object lock1 new Object(); Object lock2 new Object(); Thread t1 new Thread() { Override public void run() { synchronized (lock1) { synchronized (lock2) { // do something... } } } }; t1.start(); Thread t2 new Thread() { Override public void run() { synchronized (lock2) { synchronized (lock1) { // do something... } } } }; t2.start();不会产生环路等待的代码:约定好先获取lock1,再获取lock2,就不会环路等待。Object lock1 new Object(); Object lock2 new Object(); Thread t1 new Thread() { Override public void run() { synchronized (lock1) { synchronized (lock2) { // do something... } } } }; t1.start(); Thread t2 new Thread() { Override public void run() { synchronized (lock1) { synchronized (lock2) { // do something... } } } }; t2.start();6.synchronized关键字 - 监视器锁monitor lock6.1synchronized的特性6.2synchronized使用示例重点是第一种也是最常用的。6.3Java标准库中的线程安全类7.wait和notify7.1wait()方法7.2notify() notifyAll() 方法7.3wait和sleep的对比面试题8.多线程案例8.1单例模式面试常考单例模式是校招中最常考的设计模式之一8.2阻塞队列8.3线程池1概念线程池最大的好处就是减少每次启动、销毁线程的损耗。2模拟实现8.4定时器自行实现一个定时器虽然执行结果看起来对了但其实还是有几个问题的。9.总结

相关文章:

JAVA EE初阶 2: 多线程-初阶

1.认识线程(Thread)1.1 概念1.2 创建线程(1) 方法1 - 继承Thread类【 Ctrl C 取消 终止 】(2) 方法2 - 实现Runnable接口(3) 总结(4) 匿名内部类&#xff0…...

python --飞浆离线ocr使用/paddleocr

python 3.10.3 和 飞浆ocr 3.2使用 依赖 paddlepaddle3.2.0 paddleocr3.2.0运行测试 paddleocr ocr -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_002.png --use_doc_orientation_classify False --use_doc_unwarping False --use_text…...

稳转细胞株构建全攻略:从理论到实践的生物制药核心技术解析

为何稳转细胞株成为生物制药领域的“兵家必争之地”?稳转细胞株被称为“药物研发的基石”,是单克隆抗体、双抗、疫苗、ADC等生物制品规模化生产的核心环节,还是药物生产成本、效率和质量的关键决定因素。一株高产稳定的细胞株有可能直接缩短药…...

PHing配置文件完全手册:build.xml编写指南与示例

PHing配置文件完全手册:build.xml编写指南与示例 【免费下载链接】phing PHing Is Not GNU make; its a PHP project build system or build tool based on Apache Ant. 项目地址: https://gitcode.com/gh_mirrors/ph/phing PHing是一款基于Apache Ant的PHP项…...

laravel-menu性能优化:高效生成与缓存策略

laravel-menu性能优化:高效生成与缓存策略 【免费下载链接】laravel-menu A quick way to create menus in Laravel 项目地址: https://gitcode.com/gh_mirrors/la/laravel-menu laravel-menu是一款专为Laravel框架设计的菜单生成工具,能够帮助开…...

2026高职物联网毕业起薪一般多少?就业方向、岗位薪资与升职路径全解析

【先说结论】2026年高职物联网专业毕业生的起薪,通常会落在【4500元—8000元/月】这个区间。若在新一线、强二线城市,且具备项目经验、证书、实习经历,起点拿到【7000元以上】并不罕见;如果是制造业城市、传统中小企业&#xff0c…...

MobileCoin智能合约开发入门:构建基于隐私支付的去中心化应用

MobileCoin智能合约开发入门:构建基于隐私支付的去中心化应用 【免费下载链接】mobilecoin Private payments for mobile devices. 项目地址: https://gitcode.com/gh_mirrors/mo/mobilecoin MobileCoin是一个专注于移动设备隐私支付的开源项目,它…...

2026大专财富管理毕业工资多少?就业薪资、岗位方向与升职考证全攻略

【先说结论】2026年大专学财富管理,毕业后的起薪通常不会“一步到位特别高”,但它是一个【越做越值钱】的专业。若进入银行、证券、保险、第三方财富公司等行业,刚毕业月薪大多在【4500元—9000元】之间;如果在一线城市、业绩岗、…...

2026年低功耗国产32位MCU哪家强?业内权威评测榜单揭晓

在物联网、智能家居、可穿戴设备以及工业传感器等终端应用爆发的当下,低功耗已成为衡量微控制器(MCU)核心竞争力的关键指标之一。对于工程师和采购决策者而言,如何在琳琅满目的国产芯片中选择一款兼具“低功耗”与“高性能”的32位…...

Obsidian Importer常见问题解答:解决90%用户都会遇到的导入难题

Obsidian Importer常见问题解答:解决90%用户都会遇到的导入难题 【免费下载链接】obsidian-importer Obsidian Importer lets you import notes from other apps and file formats into your Obsidian vault. 项目地址: https://gitcode.com/gh_mirrors/ob/obsidi…...

安全优先!2026年电机微控制器 MCU 精选推荐

在工业自动化、汽车电子以及智能家居领域,电机作为核心执行单元,其控制系统的安全性与可靠性直接关系到整机设备的稳定运行与使用寿命。随着工业4.0和新能源汽车的快速发展,市场对电机微控制器(MCU)的要求已不再局限于…...

MoonShine Admin Panel完全解析:为何它是2024年最值得尝试的Laravel后台框架?

MoonShine Admin Panel完全解析:为何它是2024年最值得尝试的Laravel后台框架? 【免费下载链接】moonshine Simple for beginners and powerful for experts 项目地址: https://gitcode.com/gh_mirrors/moonsh/moonshine MoonShine Admin Panel是一…...

eslint-plugin-jest核心功能解析:为什么它是Jest测试的最佳拍档

eslint-plugin-jest核心功能解析:为什么它是Jest测试的最佳拍档 【免费下载链接】eslint-plugin-jest ESLint plugin for Jest 项目地址: https://gitcode.com/gh_mirrors/es/eslint-plugin-jest eslint-plugin-jest是专为Jest测试框架打造的ESLint插件&…...

为什么VoxFormer能减少45% GPU内存占用?揭秘高效内存管理策略

为什么VoxFormer能减少45% GPU内存占用?揭秘高效内存管理策略 【免费下载链接】VoxFormer Official PyTorch implementation of VoxFormer [CVPR 2023 Highlight] 项目地址: https://gitcode.com/gh_mirrors/vo/VoxFormer VoxFormer作为CVPR 2023 Highlight的…...

jeston orin nx系统问题汇总

1、浏览器打不开 way1:遇到浏览器打不开的问题,snap兼容性问题,可以用上面这个指令处理sudo snap revert snapd way2:https://blog.csdn.net/Flag_ing/article/details/150655043 2、摄像头导致存储爆满 step1:立即释放磁盘空间 不要直接…...

ScrewFast模板入门:如何使用这款开源Astro框架快速搭建现代网站

ScrewFast模板入门:如何使用这款开源Astro框架快速搭建现代网站 【免费下载链接】ScrewFast Open-source Astro website template with sleek, customizable TailwindCSS components. 项目地址: https://gitcode.com/gh_mirrors/sc/ScrewFast ScrewFast是一款…...

生物素 PEG 修饰谷氨酸:Fmoc-Glu (biotinyl-PEG)-OH,817169-73-6性能与应用

试剂基本信息中文名称:FMOC-GLU(生物素基-PEG)-OH,芴甲氧羰基-谷氨酰胺酸(生物素基-聚乙二醇),FMOC-L-谷氨酰胺(BIOTINYL-PEG)英文名称:Fmoc-Glu(biotinyl-PEG)-OH ,Fmoc-Glu(biotinyl-PEG)-HydroxylsCAS号&#xff1a…...

dbblog扩展开发:如何为你的博客系统添加自定义功能模块

dbblog扩展开发:如何为你的博客系统添加自定义功能模块 【免费下载链接】dbblog 基于SpringBoot2.xVue2.xElementUIIviewElasticsearchRabbitMQRedisShiro的多模块前后端分离的博客项目 项目地址: https://gitcode.com/gh_mirrors/db/dbblog dbblog是一款基于…...

MoonShine主题定制教程:20+预设模板与品牌色快速适配技巧

MoonShine主题定制教程:20预设模板与品牌色快速适配技巧 【免费下载链接】moonshine Simple for beginners and powerful for experts 项目地址: https://gitcode.com/gh_mirrors/moonsh/moonshine MoonShine是一款Simple for beginners and powerful for ex…...

PHing实战:如何使用FileSet和FilterChain处理文件操作

PHing实战:如何使用FileSet和FilterChain处理文件操作 【免费下载链接】phing PHing Is Not GNU make; its a PHP project build system or build tool based on Apache Ant. 项目地址: https://gitcode.com/gh_mirrors/ph/phing PHing是一款基于Apache Ant的…...

Obsidian Importer路线图:未来将支持哪些新功能和导入格式?

Obsidian Importer路线图:未来将支持哪些新功能和导入格式? 【免费下载链接】obsidian-importer Obsidian Importer lets you import notes from other apps and file formats into your Obsidian vault. 项目地址: https://gitcode.com/gh_mirrors/ob…...

dbblog核心技术栈揭秘:Elasticsearch+Redis+RabbitMQ如何提升博客性能

dbblog核心技术栈揭秘:ElasticsearchRedisRabbitMQ如何提升博客性能 【免费下载链接】dbblog 基于SpringBoot2.xVue2.xElementUIIviewElasticsearchRabbitMQRedisShiro的多模块前后端分离的博客项目 项目地址: https://gitcode.com/gh_mirrors/db/dbblog dbb…...

MobileCoin与传统支付对比:为什么隐私支付是未来移动金融的必然趋势

MobileCoin与传统支付对比:为什么隐私支付是未来移动金融的必然趋势 【免费下载链接】mobilecoin Private payments for mobile devices. 项目地址: https://gitcode.com/gh_mirrors/mo/mobilecoin 在数字化时代,移动支付已成为日常生活的重要组成…...

10个必须掌握的eslint-plugin-jest规则:提升测试代码健壮性

10个必须掌握的eslint-plugin-jest规则:提升测试代码健壮性 【免费下载链接】eslint-plugin-jest ESLint plugin for Jest 项目地址: https://gitcode.com/gh_mirrors/es/eslint-plugin-jest eslint-plugin-jest是一款专为Jest测试框架设计的ESLint插件&…...

深入理解eluceo iCal 2:RFC 5545规范在PHP中的实现原理

深入理解eluceo iCal 2:RFC 5545规范在PHP中的实现原理 【免费下载链接】iCal iCal-creator for PHP 项目地址: https://gitcode.com/gh_mirrors/ic/iCal eluceo iCal是一个强大的PHP库,它实现了RFC 5545规范,让开发者能够轻松创建符合…...

Inkdown高级技巧:掌握Mermaid图表与Katex公式的完美集成方法

Inkdown高级技巧:掌握Mermaid图表与Katex公式的完美集成方法 【免费下载链接】inkdown A WYSIWYG Markdown editor, improve reading and editing experience. and generate your Markdown files into online documents in the easiest and fastest way. 项目地址…...

PlayIntegrityNEXT最新更新日志:V11版本带来了哪些改进?

PlayIntegrityNEXT最新更新日志:V11版本带来了哪些改进? 【免费下载链接】PlayIntegrityNEXT 项目地址: https://gitcode.com/gh_mirrors/pl/PlayIntegrityNEXT PlayIntegrityNEXT是一款专注于提升安卓设备Play完整性验证体验的工具,…...

JdonFramework性能优化指南:从内存模型到事件处理

JdonFramework性能优化指南:从内存模型到事件处理 【免费下载链接】jdonframework Domain-Driven-Design Pub/Sub Domain-Events framework 项目地址: https://gitcode.com/gh_mirrors/jd/jdonframework JdonFramework是一个基于领域驱动设计(DDD)的事件驱动…...

NeuralPi进阶玩法:添加物理旋钮、LCD屏幕与自定义效果链的硬件扩展方案

NeuralPi进阶玩法:添加物理旋钮、LCD屏幕与自定义效果链的硬件扩展方案 【免费下载链接】NeuralPi Raspberry Pi guitar pedal using neural networks to emulate real amps and effects. 项目地址: https://gitcode.com/gh_mirrors/ne/NeuralPi NeuralPi是一…...

Venom未来roadmap:即将发布的5大令人期待的新功能

Venom未来roadmap:即将发布的5大令人期待的新功能 【免费下载链接】venom 🐍 Manage and run your integration tests with efficiency - Venom run executors (script, HTTP Request, web, imap, etc... ) and assertions 项目地址: https://gitcode.…...