当前位置:网站首页>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
边栏推荐
- 【c语言】基础篇学习笔记
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- Wechat applet calculates the distance between the two places
- Introduction to JSON usage scenarios and precautions
- 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
- [JS event -- event flow]
- Which insurance company has a better product of anti-cancer insurance?
- Force buckle 540 A single element in an ordered array
- [tips] use Matlab GUI to read files in dialog mode
- Delete the code you wrote? Sentenced to 10 months!
猜你喜欢

Hands on deep learning (II) -- multi layer perceptron
![[personal notes] PHP common functions - custom functions](/img/3d/d50622e3ddb08f654f30063e8226ac.jpg)
[personal notes] PHP common functions - custom functions

Delete the code you wrote? Sentenced to 10 months!

手撕——排序

Fluent icon demo

Raspberry pie GPIO pin controls traffic light and buzzer

FAQ | FAQ for building applications for large screen devices

JVM knowledge points

Introduction to vmware workstation and vSphere

First acquaintance with string+ simple usage (II)
随机推荐
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
Hand tear - sort
SQL:常用的 SQL 命令
LCM of Spreadtrum platform rotates 180 °
FAQ | FAQ for building applications for large screen devices
【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
pip 安装第三方库
Installation et utilisation du lac bleu
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Wpviewpdf Delphi and Net PDF viewing component
Common sense of cloud server security settings
cookie、session、tooken
How should the team choose the feature branch development mode or trunk development mode?
PIP installation of third-party libraries
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
The difference between vectorresize and reverse.
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
Recyclerview add header
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs