当前位置:网站首页>[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
边栏推荐
- [updating] wechat applet learning notes_ three
- Unity Editor Extension - Outline
- 单调栈-42. 接雨水
- Pit & ADB wireless debugging of vivo real machine debugging
- C course design employee information management system
- Find the intersection of line segments
- Encoding and decoding of golang URL
- Mall management system of database application technology course design
- Osgearth target selection
- [linear table] basic operation of bidirectional linked list specify node exchange
猜你喜欢
Unity Editor Extension - drag and drop
Animation_ IK overview
Introduction to hexadecimal coding
【Rust笔记】02-所有权
MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
Abstract classes and interfaces
Base64和Base64URL
matlab神经网络所有传递函数(激活函数)公式详解
Unity editor expansion - controls, layouts
Clion toolchains are not configured configure disable profile problem solving
随机推荐
Markdown learning
Visual Studio (VS) shortcut keys
Exe file running window embedding QT window
Unity interactive water ripple post-treatment
Abstract classes and interfaces
What is BFC?
go 解析身份证
Kunlunbase meetup is waiting for you!
Advanced OSG collision detection
Ue5 opencv plug-in use
Data analysis exercises
【Rust 笔记】10-操作符重载
Animation_ IK overview
Graphics_ Games101/202 learning notes
[K & R] Chinese Second Edition personal questions Chapter1
Golang中删除字符串的最后一个字符
Transmit pictures with Base64 encoding
Message pack in C deserializes array objects
Redis cluster series 4
Why can void * be a general pointer