当前位置:网站首页>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
边栏推荐
- MySQL advanced SQL statement 2
- Yyds dry inventory compiler and compiler tools
- 【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
- Www 2022 | rethinking the knowledge map completion of graph convolution network
- Visual slam Lecture 3 -- Lie groups and Lie Algebras
- BiShe cinema ticket purchasing system based on SSM
- LCM of Spreadtrum platform rotates 180 °
- [tips] use Matlab GUI to read files in dialog mode
- The 11th Blue Bridge Cup single chip microcomputer provincial competition
- Wechat applet pull-down loading more waterfall flow loading
猜你喜欢
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
"No war on the Western Front" we just began to love life, but we had to shoot at everything
Www 2022 | rethinking the knowledge map completion of graph convolution network
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
云服务器的安全设置常识
初识P4语言
BGP experiment the next day
随机推荐
The 10th Blue Bridge Cup single chip microcomputer provincial competition
Three ways for programmers to learn PHP easily and put chaos out of order
【c语言】基础篇学习笔记
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Installation and use of blue lake
Www2022 | know your way back: self training method of graph neural network under distribution and migration
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
go 函数
A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
Delete the code you wrote? Sentenced to 10 months!
SQL:常用的 SQL 命令
5g era is coming in an all-round way, talking about the past and present life of mobile communication
The 8th Blue Bridge Cup single chip microcomputer provincial competition
Go variables and constants
QT designer plug-in implementation of QT plug-in
uni-app - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Use of go package
[personal notes] PHP common functions - custom functions
PIP installation of third-party libraries
The 5th Blue Bridge Cup single chip microcomputer provincial competition