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

【Java学习笔记】线程基础

线程基础

一、基本概念

1. 程序

是为完成特定任务,用某种语言编写的一组指令的集合(即代码)

2. 进程

(1)进程是程序的一次执行过程,或是正在运行的一个程序,是动态过程(有它自身的产生,存在和消亡的过程)

(2)进程的启动会占用相应的资源(CPU、内存等)

(3)通俗理解:进程是指运行的程序,启动一个程序(打开一个软件)的过程就是启动一个进程,关闭程序的过程就是进程的消亡过程

3. 线程

(1)线程是由进程创建的,是进程的一个实体

(2)一个进程可以拥有多个线程

(3)举例:百度网盘同时下载多个文件,这就是多线程

4. 单线程

同一个时刻,只允许执行一个进程

5. 多线程

同一个时刻,可以执行多个线程(举例:QQ 可以同时打开多个聊天窗口)

6. 并发

(1)同一个时刻,多个任务交替执行,造成一种 “ 貌似同时 ” 的错觉

(2)实例:单核 CPU 实现的多任务就是并发

在这里插入图片描述

解释:由一个 CPU 来回切换任务,然后执行,这个过程很迅速

7. 并行

(1)同一个时刻,多个任务同时执行,多喝 CPU 可以实现并行,并发和并行

(2)实例:多个 CPU 同时存在,每个 CPU 执行自己的进程

在这里插入图片描述

注意:每个 CPU 也可以实现并发

8. 创建线程的两种方法


二、继承 Thead 类

代码示例

package Thread;public class main {public static void main(String[] args) {a a = new a();a.start();int i = 0;while (true) {System.out.println("main " + ++i + "--->" + Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 5) {break;}}}
}class a extends Thread {@Overridepublic void run() {int i = 0;while (true) {System.out.println("hello " + ++i + "--->" +  Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 10) {break;}}}
}

运行结果

main 1--->main
hello 1--->Thread-0
main 2--->main
hello 2--->Thread-0
hello 3--->Thread-0
main 3--->main
main 4--->main
hello 4--->Thread-0
main 5--->main
hello 5--->Thread-0
hello 6--->Thread-0
hello 7--->Thread-0
hello 8--->Thread-0
hello 9--->Thread-0
hello 10--->Thread-0

代码分析

(1)sleep()方法会抛出异常需要用 try - catch 包裹

(2)关系:sleep()方法休眠的单位是毫秒1000 毫秒=1 秒

(2)获取线程名称的方法:Thread.currentThread().getName()

问题:为什么调用 start()方法可以调用 run()方法,而不是直接调用 run()方法?

(1)真正启动线程的是 start()方法,而不是 run()方法,run()方法是普通方法,会造成线程阻塞

start()方法的底层探究

1. 首先 start()方法会调用 start0()方法

2. start0()方法是native方法,由 JVM 机调用

3. 何时启动线程还需根据其他依据综合考虑

在这里插入图片描述

(2)如调用 run()方法,并不会启动线程,而是等 run()方法执行完之后才会启动 main 方法的调用

public class main {public static void main(String[] args) {a a = new a();a.run();int i = 0;while (true) {System.out.println("main " + ++i + "--->" + Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 5) {break;}}}
}class a extends Thread {@Overridepublic void run() {int i = 0;while (true) {System.out.println("hello " + ++i + "--->" +  Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 10) {break;}}}
}

运行结果

hello 1--->main
hello 2--->main
hello 3--->main
hello 4--->main
hello 5--->main
hello 6--->main
hello 7--->main
hello 8--->main
hello 9--->main
hello 10--->main
main 1--->main
main 2--->main
main 3--->main
main 4--->main
main 5--->main

结论:这里 run()方法的线程名就是 main,即调用 run()方法并没有启动线程

注意:主线程的结束并不会导致进程的结束,需要等到所有线程都执行完成进程才会退出


三、实现 Runnable 接口

引出:由于 Java 是单继承机制,如果类继承了其他类,这个时候可以通过实现接口的方式创建线程

1. 代码示例

package Thread;public class main {public static void main(String[] args) {a a = new a();Thread thread = new Thread(a);thread.start();int i = 0;while (true) {System.out.println("main " + ++i + "--->" + Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 5) {break;}}}
}class a implements Runnable {@Overridepublic void run() {int i = 0;while (true) {System.out.println("hello " + ++i + "--->" +  Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}if (i == 10) {break;}}}
}

运行结果

main 1--->main
hello 1--->Thread-0
main 2--->main
hello 2--->Thread-0
hello 3--->Thread-0
main 3--->main
main 4--->main
hello 4--->Thread-0
hello 5--->Thread-0
main 5--->main
hello 6--->Thread-0
hello 7--->Thread-0
hello 8--->Thread-0
hello 9--->Thread-0
hello 10--->Thread-0

代码分析

(1)实现了 Runnable 接口,但是这个接口中并没有 start()方法,不可以直接调用

(2)通过把 a 对象作为参数传入构造器中,调用构造器创建 Thread 对象,调用 Thread 对象的 start()方法

2. 问题:为什么调用 Thread 的 start()方法就可以调用 a 类的 run()方法?

回答:底层采用了静态代理模式

理解:既然 Runnable 接口没有 start()方法,那就借助 Thread 类,让它来实现 start()方法(体现代理

3. 模拟 Thread 类,理解实现原理

public class main {public static void main(String[] args) {test test = new test();Thread_proxy thread_proxy = new Thread_proxy(test);thread_proxy.start();}
}class Thread_proxy implements Runnable{private Runnable target = null;// 构造器public Thread_proxy(Runnable target) {this.target = target;}// 实现 run() 方法@Overridepublic void run() {if(target != null){target.run();}}public void start(){start0();}public void start0(){run();}
}class test implements Runnable{@Overridepublic void run() {System.out.println("调用了 test 类的 run() 方法");}
}

代码分析

(1)创建 Thread_proxy 类,实现 Runnable()接口,模拟 Thead 类

(2)创建 test 类,实现 Runnable()接口

(3)在主函数中创建 Thread_proxy 对象,在构造器中传入 test 对象,调用 Thread_proxy 对象的 start()方法来启动线程

底层分析

1. 为什么可以传入 test?

因为 test 是实现了 Runnable 接口的一个类,根据接口的多态,接口类型可指向实现该接口的类对象

2. 如何实现调用 test 类的 run()方法的?

(1)thread_proxy 调用 start()方法,最终调用 start0()方法,该方法调用该类中的 run()方法

(2)此时 target 不为空(target 指向了传入的 test 对象),根据动态绑定机制,这个时候会调用 test 类的 run()方法

http://www.lryc.cn/news/572624.html

相关文章:

  • C++实例化对象与初始化的区别:深入解析与最佳实践
  • EfficientVLA:面向视觉-语言-动作模型无训练的加速与压缩
  • 准备开始适配高德Flutter的鸿蒙版了
  • 观远ChatBI:加速零售消费企业数据驱动的敏捷决策
  • 以太坊节点搭建私链(POA)
  • 【秒杀系统设计】
  • Vue3+TypeScript+ Element Plus 从Excel文件导入数据,无后端(点击按钮,选择Excel文件,由前端解析数据)
  • 拓客软件有哪些?
  • AI Agent开发与安全
  • 企业级文档搜索系统架构设计与实践指南
  • 巧用云平台API实现开源模型免费调用的实战教程
  • 数据库从零开始:MySQL 中的 DDL 库操作详解【Linux版】
  • 从生活场景学透 JavaScript 原型与原型链
  • 链接过程使用链接器将该目标文件与其他目标文件、库文件、启动文件等链接起来生成可执行文件。附加的目标文件包括静态连接库和动态连接库。其中的启动文件是什么意思?
  • 【内存】Linux 内核优化实战 - vm.max_map_count
  • Spring AOP @AfterReturning (返回通知)的使用场景
  • MySQL 分页查询列表;Explain ;深度分页 ;管理系统,筛选系统
  • AR 眼镜之-条形码识别-实现方案
  • 【AI时代速通QT】第二节:Qt SDK 的目录介绍和第一个Qt Creator项目
  • AI人工智能与LLM大语言模型有什么区别
  • Node.js 在前端开发中的作用与 npm 的核心理解
  • 1.22Node.js 中操作 Redis
  • Kafka线上集群部署方案:从环境选型到资源规划思考
  • 源易信息:领先GEO供应商的市场布局与服务优势
  • 【生活点滴】车辆过户、新车挂牌
  • 变幻莫测:CoreData 中 Transformable 类型面面俱到(五)
  • 学习华为 ensp 的学习心得体会
  • 百胜软件荣膺零售商业评论“《2024创新零售》优秀服务商TOP”奖项
  • 学习华为 ensp 的学习心得体会(适合新手)
  • Python 数据分析与可视化 Day 2 - 数据清洗基础