当前位置:网站首页>Playing with concurrency: what are the ways of communication between threads?
Playing with concurrency: what are the ways of communication between threads?
2022-07-02 04:05:00 【Java knowledge hall】
Shared memory
volatile keyword
Wait for a notice
wait,notify Why do we have to synchronized Use it together ?
When there is no resource for the thread to obtain , Thread needs to wait , Because there may be multiple threads waiting , So you need to put it into the synchronization queue of resources . When resources meet , Need to wake up the thread in the waiting queue . When multiple threads operate on a synchronized queue , To ensure thread safety , So it needs to be locked
sleep and wait What are the differences ?
although sleep and wait All the behaviors are waiting , But absolutely 2 There are two unrelated methods .
- sleep It's a simple waiting method , and wait Is a method for thread communication , When resources are not satisfied, we have to wait . So this 2 Methods are placed in different classes ,sleep yes Thread Class method ,wait yes Object Class method .
- sleep Method can be used anywhere ,wait Only in synchronized Method or synchronized Block the use of
- Thread.sleep Just give up CPU, Object locks are not released
- Object.wait Not only give up CPU, It also releases the object lock . Only call... For this object notify() It's time or method , To execute... Again
wait Why release the lock when
When a thread a Get into synchronized(obj) In block , That's right obj be locked , Call at this time wait It will be blocked , If the lock is not released at this time , Then other threads will not enter synchronized In block , Naturally, you can't call notify Method , This will cause deadlock
边栏推荐
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- JVM知识点
- Where can I buy cancer insurance? Which product is better?
- go 包的使用
- 【c语言】动态规划---入门到起立
- [wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
- Go variables and constants
- 【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
- The original author is out! Faker. JS has been controlled by the community..
- First acquaintance with P4 language
猜你喜欢
蓝湖的安装及使用
Introduction to vmware workstation and vSphere
【IBDFE】基于IBDFE的频域均衡matlab仿真
QT designer plug-in implementation of QT plug-in
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
【leetcode】74. Search 2D matrix
藍湖的安裝及使用
初识P4语言
SQL Yiwen get window function
随机推荐
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
The 8th Blue Bridge Cup single chip microcomputer provincial competition
Fluent icon demo
The 9th Blue Bridge Cup single chip microcomputer provincial competition
go 语言命名规范
Basic operations of MySQL database (based on tables)
The confusion I encountered when learning stm32
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
The 7th Blue Bridge Cup single chip microcomputer provincial competition
cookie、session、tooken
蓝湖的安装及使用
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
Wechat applet JWT login issue token
Introduction to JSON usage scenarios and precautions
Feature Engineering: summary of common feature transformation methods
Go variables and constants
Handling of inconsistency between cursor and hinttext position in shutter textfield
Hand tear - sort
The fourth provincial competition of Bluebridge cup single chip microcomputer