当前位置:网站首页>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
边栏推荐
- Wechat applet JWT login issue token
- Force buckle 540 A single element in an ordered array
- MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
- The 6th Blue Bridge Cup single chip microcomputer provincial competition
- Li Kou interview question 02.08 Loop detection
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Go语言介绍
- 【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
- Which insurance company has a better product of anti-cancer insurance?
- 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
猜你喜欢

【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真

go 包的使用

初识P4语言

【c语言】基础篇学习笔记

Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf

Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000

整理了一份ECS夏日省钱秘籍,这次@老用户快来领走

WiFi 5GHz frequency

FAQ | FAQ for building applications for large screen devices

BGP experiment the next day
随机推荐
The first practical project of software tester: web side (video tutorial + document + use case library)
u本位合约爆仓清算解决方案建议
LxC limits the number of CPUs
5G时代全面到来,浅谈移动通信的前世今生
"No war on the Western Front" we just began to love life, but we had to shoot at everything
Monkey测试
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
[tips] use Matlab GUI to read files in dialog mode
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
BGP experiment the next day
Go variables and constants
cookie、session、tooken
Li Kou interview question 02.08 Loop detection
Force buckle 540 A single element in an ordered array
Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
【c语言】基础篇学习笔记
SQL:常用的 SQL 命令
【leetcode】81. Search rotation sort array II
[untitled]
云服务器的安全设置常识