当前位置:网站首页>[concurrent programming] thread foundation and sharing between threads
[concurrent programming] thread foundation and sharing between threads
2022-07-03 08:32:00 【keeper42】
2022.02.08
Thread basis
CPU The relationship between the number of cores and the number of threads
The core number : Number of threads =1:1 ; After using hyper threading technology ---> 1:2
CPU Time slice rotation mechanism
also called RR Dispatch , Will cause context switching
What are processes and threads
process : The smallest unit of program running resource allocation , There are multiple threads inside the process , Will share the resources of this process
Threads :CPU Minimum unit of scheduling , Must exist depending on the process .
Clarify parallelism and concurrency
parallel : At the same time , The ability to handle things at the same time
Concurrent : Related to unit time , The ability to handle things per unit of time
The significance of high concurrency programming 、 Benefits and precautions
benefits : make the best of cpu Resources for 、 Speed up user response time , Program modularization , Asynchronism
problem :
Threads share resources , There is a conflict ;
It is easy to cause deadlock ;
Enable too many threads , It's possible to break the machine
know Java Thread in
Threads
Thread vs Runnable
A new way to start threads
Three
How can we make Java The threads in stop working safely
Thread natural termination : Execute naturally or throw unhandled exception
stop(),resume(),suspend() It is no longer recommended to use ,stop() It will cause the thread not to release resources correctly ,suspend() It is easy to cause deadlock .
java Threads are collaborative , Not preemptive
Call a thread's interrupt() Method to interrupt a thread , It's not forcing the thread to close , Just say hello to this thread , Set the thread interrupt flag position to true, Whether the thread is interrupted , It's up to the thread itself to decide .
isInterrupted() Determines whether the current thread is in an interrupted state .
static Method interrupted() Determines whether the current thread is in an interrupted state , At the same time, the interrupt flag bit is changed to false.
If the method throws InterruptedException, The thread's interrupt flag bit will be reset to false, If you really need to interrupt the thread , Ask ourselves to catch Call... Again in the statement block interrupt().
Thread common methods and thread status
Thread only 5 States . The whole life cycle is the switching of these States .
run() and start() :run Methods are ordinary methods of ordinary objects , Only called start() after ,Java Will map the thread object to the actual thread in the operating system , We'll do it again run Method .
yield() : Give up cpu The enforcement of , Turn the thread from running to runnable , But the next time slice , The thread may still be selected to run again .
Thread priority
The value is 1~10, Default is 5, But thread priority is unreliable , Not recommended as a means of thread development
The guardian thread
Die with the main thread ,finally There is no guarantee that
Sharing between threads
synchronized Built in lock
Object lock , The lock is an object instance of a class .
Kind of lock , The lock is for each class Class object , Of each class Class There is only one object in a virtual machine , So there is only one class lock .
volatile keyword
The lightest synchronization mechanism , Suitable for only one thread to write , Multi thread reading scenario , Because it can only ensure visibility .
ThreadLocal
Thread variable . It can be understood as a map, type Map<Thread,Integer>
Spring When dealing with transactions ThreadLocal, Make each thread keep its own connection .
Each thread has a copy of the variable (hash) Thread isolation , Synchronization of variables is not guaranteed , Only ensure the isolation of variables
ThreadLocal Thread unsafe , Causes a memory leak ( Weak reference )
virtual machine /JVM The underlying knowledge ( Strong citation 、 Soft citation 、 Weak reference 、 Virtual reference )
Object o = new Object(); // Strong citation : Stack frame ---》 Heap instance
SoftRefence To overflow

边栏推荐
- Unity multi open script
- Minimap plug-in
- Unity Editor Extension - event handling
- 了解小程序的笔记 2022/7/3
- Delete the last character of the string in golang
- 【Rust 笔记】10-操作符重载
- Why can void * be a general pointer
- Image processing 8-cnn image classification
- UE4 source code reading_ Bone model and animation system_ Animation process
- Redis data structure
猜你喜欢
![P1596 [USACO10OCT]Lake Counting S](/img/a7/07a84c93ee476788d9443c0add808b.png)
P1596 [USACO10OCT]Lake Counting S

MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板

jupyter远程服务器配置以及服务器开机自启

About Wireshark's unsuccessful installation of npcap

Redis的数据结构

php-fpm软件的安装+openresty高速缓存搭建

Base64编码简介

Basic operation and process control

ArrayList

Simple demo of solving BP neural network by gradient descent method
随机推荐
Cesium for unreal quick start - simple scenario configuration
C#课程设计之学生教务管理系统
KunlunBase MeetUP 等您来!
Sequence of map implementation classes
Unity4.3.1 engine source code compilation process
【Rust 笔记】13-迭代器(上)
【Rust笔记】05-错误处理
數據庫應用技術課程設計之商城管理系統
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Unity editor expansion - draw lines
Base64编码简介
Some understandings of 3dfiles
redis集群系列四
Osgconv tool usage
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Markdown learning
UE4 call DLL
[K & R] Chinese Second Edition personal questions Chapter1
Mall management system of database application technology course design
【Rust 笔记】08-枚举与模式