当前位置:网站首页>[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

边栏推荐
- Development material set
- 【Rust笔记】05-错误处理
- Golang time format sorting
- jupyter远程服务器配置以及服务器开机自启
- Location of package cache downloaded by unity packagemanager
- Get to know unity2 for the first time
- Basic operation and process control 2
- Three characteristics
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- Encoding and decoding of golang URL
猜你喜欢

KunlunBase MeetUP 等您来!

C#课程设计之学生教务管理系统

Kunlunbase meetup is waiting for you!

100 GIS practical application cases (78) - Multi compliance database design and data warehousing

Notes on understanding applets 2022/7/3

Image processing 8-cnn image classification

Redis的数据结构

UE4 source code reading_ Bone model and animation system_ Animation node

Collection interface

Dotween plug-in
随机推荐
Downward compatibility and upward compatibility
【云原生】微服务之Feign的介绍与使用
Conversion between string and int types in golang
Unity Editor Extension - Outline
Easy touch plug-in
Unity change default editor
Osgearth starry background
C course design employee information management system
Clion toolchains are not configured configure disable profile problem solving
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
Vscode, idea, VIM development tool shortcut keys
Kunlunbase meetup is waiting for you!
Constraintlayout's constraintset dynamically modifies constraints
Student educational administration management system of C # curriculum design
Development experience and experience
Message pack in C deserializes array objects
2021-10-19
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Collection interface
【K&R】中文第二版 个人题解 Chapter1