当前位置:网站首页>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
边栏推荐
- PR zero foundation introductory guide note 2
- The 5th Blue Bridge Cup single chip microcomputer provincial competition
- [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!
- Is the product of cancer prevention medical insurance safe?
- 【c语言】基础篇学习笔记
- [Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- The 7th Blue Bridge Cup single chip microcomputer provincial competition
- Go language introduction
- Where can I buy cancer insurance? Which product is better?
猜你喜欢

The 9th Blue Bridge Cup single chip microcomputer provincial competition

Go语言介绍

C语言:逻辑运算和判断选择结构例题

【leetcode】34. Find the first and last positions of elements in a sorted array
![[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!](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[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!

Dare to go out for an interview without learning some distributed technology?

Basic operations of MySQL database (based on tables)
![[ibdfe] matlab simulation of frequency domain equalization based on ibdfe](/img/a1/441f400668e736b70cb36443f2267a.png)
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe

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

毕设-基于SSM电影院购票系统
随机推荐
LxC limits the number of CPUs
Monkey test
JVM knowledge points
5G时代全面到来,浅谈移动通信的前世今生
5g era is coming in an all-round way, talking about the past and present life of mobile communication
[personal notes] PHP common functions - custom functions
Www2022 | know your way back: self training method of graph neural network under distribution and migration
Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
C language: examples of logical operation and judgment selection structure
蓝湖的安装及使用
10 minutes to understand CMS garbage collector in JVM
The 6th Blue Bridge Cup single chip microcomputer provincial competition
Visual slam Lecture 3 -- Lie groups and Lie Algebras
Go语言介绍
The 9th Blue Bridge Cup single chip microcomputer provincial competition
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
Déchirure à la main - tri
初识P4语言
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
QT designer plug-in implementation of QT plug-in