当前位置:网站首页>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
边栏推荐
- Hands on deep learning (II) -- multi layer perceptron
- 云服务器的安全设置常识
- pip 安装第三方库
- 10 minutes to understand CMS garbage collector in JVM
- First acquaintance with P4 language
- Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
- The 8th Blue Bridge Cup single chip microcomputer provincial competition
- 【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
- okcc为什么云呼叫中心比传统呼叫中心更好?
- JVM knowledge points
猜你喜欢

The first practical project of software tester: web side (video tutorial + document + use case library)

【c语言】动态规划---入门到起立

Qt插件之Qt Designer插件实现

Use a mask to restrict the input of the qlineedit control

树莓派GPIO引脚控制红绿灯与轰鸣器

WiFi 5GHz frequency

Sword finger offer II 006 Sort the sum of two numbers in the array

The fourth provincial competition of Bluebridge cup single chip microcomputer

JVM知识点

云服务器的安全设置常识
随机推荐
Suggestions on settlement solution of u standard contract position explosion
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
Use a mask to restrict the input of the qlineedit control
The original author is out! Faker. JS has been controlled by the community..
Wpviewpdf Delphi and Net PDF viewing component
XSS prevention
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Delete the code you wrote? Sentenced to 10 months!
JVM knowledge points
Today's plan: February 15, 2022
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
[JS -- map string]
【leetcode】34. Find the first and last positions of elements in a sorted array
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
The first practical project of software tester: web side (video tutorial + document + use case library)
Déchirure à la main - tri
The 10th Blue Bridge Cup single chip microcomputer provincial competition
The 5th Blue Bridge Cup single chip microcomputer provincial competition
Monkey测试
Homework in Chapter 3 of slam course of dark blue vision -- derivative application of T6 common functions