当前位置:网站首页>On "waiting for awakening mechanism"
On "waiting for awakening mechanism"
2022-04-22 20:47:00 【Piink】
Wait for the wake-up mechanism
Inter thread communication :
Multiple threads are processing the same resource , But the action of handling ( Thread tasks ) But not the same
Why handle inter thread communication ?
When multiple threads execute concurrently , By default CPU It's random thread switching , When we need multiple threads to complete a task together , And I hope they will implement it regularly , So many threads need some coordinated communication , In order to help us achieve multi-threaded common operation of a piece of data
How to ensure inter thread communication and make effective use of resources ?
Multiple threads are processing the same resource , And the tasks are different , Thread communication is needed to help solve the problem between threads
The use or operation of the same variable . When multiple threads operate on the same data , Avoid contention for the same shared variable . That is, we need to use certain means to make each thread make effective use of resources , And that means ---- Wait for the wake-up mechanism
Wait for the wake-up mechanism

Waiting for wake-up mechanism is actually a classic “ Producers and consumers ” The problem of
Understand the waiting wake-up mechanism with buying and selling steamed stuffed buns
Waiting for wake-up cases : Communication between threads
Create a customer thread ( consumer ): Tell the boss the type and quantity of steamed buns to buy , call wait Method , give up cpu Implementation , Enter into WAITING state ( Wait indefinitely )
Create a boss thread ( producer ): Spend some time making steamed stuffed buns , After making the steamed stuffed bun , call notify Method , Wake up customers to eat steamed stuffed buns
Be careful :
Customer and boss threads must be wrapped in synchronized code blocks , Ensure that only one of the wait and wake-up can be performed
The lock object used in synchronization must be unique
Only lock objects can call wait and notify Method
Object Methods in class
void wait() This object is called in another thread notify() Method or notifyAll() Before the method , Causes the current thread to wait
void notify() Wake up a single thread waiting on this object monitor , Will continue to execute wait The code after the method
public static void main(String[] args) {
// Create lock object , Ensure that the only
Object obj=new Object();
// Create a customer thread ( consumer )
new Thread(){
@Override
public void run(){
// Ensure that only one thread waiting and waking up can execute , Need to use synchronization technology
//synchronized Synchronization means
// while(true) loop I've been waiting to buy steamed stuffed buns
// {
synchronized (obj){
System.out.println(" Tell the boss the type and quantity of steamed buns you want ");
// call wait Method , give up cpu Implementation , Enter into WAITING state ( Wait indefinitely )
try
{
obj.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
// Code executed after wake-up
System.out.println(" The steamed stuffed bun has been made , You can eat ");
}
// }
}
}.start();
// Create a boss thread ( producer )
new Thread(){
@Override
public void run(){
// Ensure that only one thread waiting and waking up can execute , Need to use synchronization technology
// call wait Method , give up cpu Implementation , Enter into WAITING state ( Wait indefinitely )
try
{
Thread.sleep(5000);// cost 5 Make steamed stuffed bun in seconds
}catch(InterruptedException e){
e.printStackTrace();
}
synchronized (obj){
System.out.println(" Boss 5 Make steamed stuffed buns in seconds , Tell the customer ");
// After making the steamed stuffed bun , call notify() Method , Wake up customers to eat steamed stuffed buns
obj.notify();
}
}
}.start();
}
版权声明
本文为[Piink]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222046150944.html
边栏推荐
猜你喜欢

Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst

联想电脑管家图文介绍:联想电脑管家怎么下载?

LeetCode HOT 100(买卖股票的最佳时机)

如何让机器人更像“人”,让slam更灵活?

L1-046 divide singles
Ali Interviewer: you'd better not write glide on your resume. It's not as simple as asking for the source code

Detailed explanation of sorting methods (8 kinds) - bucket sorting
![buuctf-[Flask]SSTI](/img/62/f272848dd18b75fd837621458b573c.png)
buuctf-[Flask]SSTI
阿里面试官:简历上最好不要写Glide,不是问源码那么简单

MySQL 进阶 存储过程 存储函数 -- 存储过程介绍、存储过程基本语法、变量(系统变量、用户定义变量、局部变量)、if、参数、case、while、repeat、loop、游标、条件处理程序
随机推荐
Better implementation ideas based on MySQL in some scenarios (continuous update)
Design and analysis of MySQL high availability architecture
Postman tests the correct posture of array, list and map input APIs
虚拟机搭建安装Pulsar环境教程(开发测试使用)
MySQL Chapter 8 Database Constraints
TikTok+Shopify独立站测评,卖家新风口.
@Requestmapping get request parameters
A thorough explanation of the future form, development status and Prospect of Business Intelligence BI | recommended collection
MySQL learning notes
Leetcode222. Number of nodes of complete binary tree
LeeCode 130. 被围绕的区域
剑指offer汇总
Leetcode-92-reverse linked list
MySQL troubleshooting information query (continuous update)
MySQL的explain,你真的会用吗?
Asynchronous replication of MySQL master-slave replication
华为机试题——HJ62 查找输入整数二进制中1的个数
List的使用
The annual salary is 170W. Alibaba P8 blind date requires the woman's monthly salary of 10000. Netizen: it's a little high
[interview ordinary people vs Expert Series] please talk about the network quadruple