当前位置:网站首页>线程和进程的关系和区别是什么
线程和进程的关系和区别是什么
2022-04-23 05:46:00 【OceanKeeper1215】
线程定义
线程是进程的基本执行单元,一个进程的所有任务都在线程中执行
进程要想执行任务,必须得有线程,进程至少要有一条线程
程序启动会默认开启一条线程,这条线程被称为主线程或 UI 线程
进程定义
进程是指在系统中正在运行的一个应用程序
每个进程之间是独立的,每个进程均运行在其专用的且受保护的内存
进程与线程的区别
地址空间:同一进程的线程共享本进程的地址空间,而进程之间则是独立的地址空间。
资源拥有:同一进程内的线程共享本进程的资源(如内存、I/O、cpu等),但是进程之间的资源是独立的。
一个进程崩溃后,在保护模式下不会对其他进程产生影响,但是一个线程崩溃整个进程都死掉。所以多进程要比多线程健壮。
进程切换时,消耗的资源大,效率高。所以涉及到频繁的切换时,使用线程要好于进程。同样如果要求同时进行并且又要共享某些变量的并发操作,只能用线程不能用进程
执行过程:每个独立的进程有一个程序运行的入口、顺序执行序列和程序入口。但是线程不能独立执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
线程是处理器调度的基本单位,但是进程不是。
优点
能适当提高程序的执行效率
能适当提高资源的利用率(CPU,内存)
线程上的任务执行完成后,线程会自动销毁
缺点
开启线程需要占用一定的内存空间(默认情况下,每一个线程都占 512 KB)
如果开启大量的线程,会占用大量的内存空间,降低程序的性能
线程越多,CPU 在调用线程上的开销就越大
程序设计更加复杂,比如线程间的通信、多线程的数据共享
版权声明
本文为[OceanKeeper1215]所创,转载请带上原文链接,感谢
https://blog.csdn.net/OceanKeeper1215/article/details/121276999
边栏推荐
- lambda expressions
- Framework analysis 2 Source code - login authentication
- St table template
- 自动控制(韩敏版)
- H. Are You Safe? Convex hull naked problem
- Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
- Implementation of displaying database pictures to browser tables based on thymeleaf
- 2. Average length of words
- In depth understanding of the relationship between dncblevel and noise denoising in the paper
- 线性代数第三章-矩阵的初等变换与线性方程组
猜你喜欢
Create binary tree
Techniques et principes de détection
C language file operation
線性代數第二章-矩陣及其運算
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
自動控制(韓敏版)
Export of data
Definition of C class and method
List segmentation best practices
随机推荐
container
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
Best practices for MySQL storage time
JDBC operation transaction
JDBC connection database
Implementation of displaying database pictures to browser tables based on thymeleaf
Problems and solutions of database migration
MySQL table constraints and table design
[untitled] database - limit the number of returned rows
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
[leetcode 459] duplicate substring
6.Reversal
卡尔曼滤波与惯性组合导航
JSP syntax and JSTL tag
线代第四章-向量组的线性相关
Database - sorting data
ValueError: loaded state dict contains a parameter group that doesn‘t match the size of optimizer‘s
2. Devops sonar installation
@Problems caused by internal dead loop of postconstruct method
Code neat way to learn