当前位置:网站首页>浅学 “等待唤醒机制 ”
浅学 “等待唤醒机制 ”
2022-04-22 20:46:00 【Piink】
等待唤醒机制
线程间通信:
多个线程在处理同一个资源,但是处理的动作(线程的任务)却不相同
为什么要处理线程间通信?
多个线程并发执行时,在默认情况下CPU是随机切换线程的, 当我们需要多个线程来共同完成一个任务,并且希望他们有规律的执行,那么多线程之间需要一些协调通信,以此来帮我们达到多线程共同操作一份数据
如何保证线程间通信有效利用资源?
多个线程在处理同一个资源,并且任务不同时,需要线程通信来帮助解决线程之间
对同一个变量的使用或操作。就是多个线程在操作同一份数据时,避免对同一共享变量的争夺。也就是我们需要通过一定的手段使各个线程能有效的利用资源,而这种手段即----等待唤醒机制
等待唤醒机制

等待唤醒机制其实就是经典的“生产者与消费者”的问题
用买卖包子理解等待唤醒机制
等待唤醒案例:线程之间的通信
创建一个顾客线程(消费者):告知老板要买的包子的种类和数量,调用wait方法,放弃cpu的执行,进入到WAITING状态(无限等待)
创建一个老板线程(生产者):花一定时间做包子,做好包子之后,调用notify方法,唤醒顾客吃包子
注意:
顾客和老板线程必须使用同步代码块包裹起来,保证等待和唤醒只能有一个在执行
同步使用的锁对象必须保证唯一
只有锁对象才能调用wait和notify方法
Object类中的方法
void wait()在其他线程调用此对象的notify()方法或notifyAll()方法前,导致当前线程等待
void notify()唤醒在此对象监视器上等待的单个线程,会继续执行wait方法之后的代码
public static void main(String[] args) {
//创建锁对象,保证唯一
Object obj=new Object();
//创建一个顾客线程(消费者)
new Thread(){
@Override
public void run(){
//保证等待和唤醒的线程只能有一个执行,需要使用同步技术
//synchronized 同步的意思
// while(true) 循环 一直等着买包子
// {
synchronized (obj){
System.out.println("告知老板要的包子的种类和数量");
//调用wait方法,放弃cpu的执行,进入到WAITING状态(无限等待)
try
{
obj.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
//唤醒之后执行的代码
System.out.println("包子已经做好了,可以吃了");
}
// }
}
}.start();
//创建一个老板线程(生产者)
new Thread(){
@Override
public void run(){
//保证等待和唤醒的线程只能有一个执行,需要使用同步技术
//调用wait方法,放弃cpu的执行,进入到WAITING状态(无限等待)
try
{
Thread.sleep(5000);//花费5秒做包子
}catch(InterruptedException e){
e.printStackTrace();
}
synchronized (obj){
System.out.println("老板5秒钟之后做好包子,告知顾客");
//做好包子之后,调用notify()方法,唤醒顾客吃包子
obj.notify();
}
}
}.start();
}
版权声明
本文为[Piink]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Piink/article/details/124330976
边栏推荐
- Write ppt with markdown
- MySQL 第8章 数据库约束
- 东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?
- Postman tests the correct posture of array, list and map input APIs
- Design and analysis of MySQL high availability architecture
- [dasctf OCT x] lost magic girl
- Improving fee shot part segmentation using course supervision
- MySQL storage engine
- Ordinary functions as friends (using examples to solve friend functions)
- Use of swift protocol
猜你喜欢

智慧农业APP软件应用价值

智慧农业成为发展道路,充分发挥智能化,解放人力

显示实现接口和隐式实现接口的区别

CDH6. 3.2 enable Kerberos authentication
十月的Android面试之旅,惨败在字节三面,幸斩获小米Offer
Ali Interviewer: you'd better not write glide on your resume. It's not as simple as asking for the source code

L1-046 整除光棍

2022 civil construction worker's question bank precision small question bank construction hall constructor

A thorough explanation of the future form, development status and Prospect of Business Intelligence BI | recommended collection

Recommended chrome plug-ins
随机推荐
Huawei machine test question - hj62 find the number of 1 in the input integer binary
Dialogue: Shen Si L, founder of papaya mobile, from Silicon Valley to Beijing
Add / remove / filter / sort array elements
Ordinary functions as friends (using examples to solve friend functions)
The list of SCI / SSCI journals has been updated and these journals have been eliminated~
How to make robots more like "people" and make slams more flexible?
MySQL主从复制之异步复制
MySQL高可用架构设计分析
I neglected to prepare for this interview, which made me beat the day before yesterday
Four ways to create threads
Use constant member functions for constant types (design a date class and time)
LeeCode 130. Surrounded area
Tiktok + Shopify independent station evaluation, seller's new outlet
Countdown case
JWT token practice and problem solving
MySQL development skills
Dynamic database tool -- database inspector
buuctf-[Flask]SSTI
Use of swift protocol
MySQL的explain,你真的会用吗?