当前位置:网站首页>Use and principle of wait notify
Use and principle of wait notify
2022-07-01 05:09:00 【People below two meters are mortals】
List of articles
1、API Introduce
- obj.wait() Let in object Monitor thread to waitSet wait for
- obj.wait(long timeout) Let in object Monitor thread to waitSet wait for , But there will be a time limit , No unlimited waiting
- obj.notify() stay object It's going on waitSet Pick one of the waiting threads to wake up
- obj.notifyAll() Give Way object It's going on waitSet All the waiting threads wake up
They are all means of collaboration between threads , All belong to Object Object method . You must get the lock for this object ( Become Owner), To call these methods
such as :
private final static Object OBJ = new Object();
@SneakyThrows
public static void main(String[] args) {
OBJ.wait();
}
The monitor status that reports an error is abnormal , That is, the current thread has not obtained OBJ lock , perform wait I don't know where to go WaitSet? It should be used in this way
private final static Object OBJ = new Object();
@SneakyThrows
public static void main(String[] args) {
synchronized (OBJ) {
OBJ.wait();
}
}
Example
private final static Object OBJ = new Object();
@SneakyThrows
public static void main(String[] args) {
new Thread(() -> {
synchronized (OBJ) {
log.info(" perform ....");
try {
OBJ.wait(); // Let the thread in OBJ Keep waiting on
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("t1 Other code ....");
}
}, "t1").start();
new Thread(() -> {
synchronized (OBJ) {
log.info(" perform ....");
try {
OBJ.wait(); // Let the thread in OBJ Keep waiting on
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("t2 Other code ....");
}
}, "t2").start();
// The main thread executes in two seconds
TimeUnit.SECONDS.sleep(2);
log.info(" Wake up the OBJ On other threads ");
synchronized (OBJ) {
OBJ.notify(); // Wake up the OBJ Last thread
}
}
You can find ,notify You can only wake up at a time WaitSet The random one in , If necessary t1 and t2 Wake up at the same time , have access to notiyAll, Will all WaitSet The threads in all wake up
2、 principle
Review the following diagram
- Owner Thread discovery condition not met , call wait Method , You can enter WaitSet Turn into WAITING state
- BLOCKED and WAITING All threads are blocked , Not occupy CPU Time slice
- BLOCKED Thread will be Owner Wake up when thread releases lock
- WAITING Thread will be Owner Thread calls notify or notifyAll Wake up when , But waking up doesn't mean the person gets the lock immediately , Still need to enter EntryList Re compete
3、wait and sleep The difference between
- sleep yes Thread Method , and wait yes Object Methods
- sleep There's no need to force and synchronized In combination with , but wait Need and synchronized Together with
- sleep While sleeping , Object locks will not be released , but wait The object lock is released while waiting
- Their states are TIMED_WAITING
4、 Use posture correctly
Here we simulate a scene , Now there is a studio ( lock ), There is a man, Xiao Nan , He has a lot of problems , It must have smoke to work , Let's make it happen
static final Object ROOM = new Object();
static boolean hasCigarette = false;
@SneakyThrows
public static void main(String[] args) {
new Thread(() -> {
synchronized (ROOM) {
log.info(" Do you have any cigarettes ?[{}]", hasCigarette);
if (!hasCigarette) {
log.info(" No smoke , Take a break !");
try {
ROOM.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info(" Do you have any cigarettes ?[{}]", hasCigarette);
if (hasCigarette) {
log.info(" You can start working ");
}
}
}, " Xiaonan ").start();
for (int i = 0; i < 5; i++) {
new Thread(() -> {
synchronized (ROOM) {
log.info(" You can start working ");
}
}, " Other people ").start();
}
TimeUnit.SECONDS.sleep(1);
new Thread(() -> {
synchronized (ROOM) {
ROOM.notify();
hasCigarette = true;
log.info(" Here comes the smoke !");
}
}, " The cigarette man ").start();
}
But there is still a problem with the current program , If there are other threads wait What shall I do? ? It is not easy to wake up the wrong thread ( spurious wakeup )? such as , Now there is another man named little girl , She's strange, too , Only when the takeout arrives can he work
static final Object ROOM = new Object();
static boolean hasCigarette = false;
static boolean hasTakeout = false;
@SneakyThrows
public static void main(String[] args) {
new Thread(() -> {
synchronized (ROOM) {
log.info(" Do you have any cigarettes ?[{}]", hasCigarette);
if (!hasCigarette) {
log.info(" No smoke , Take a break !");
try {
ROOM.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info(" Do you have any cigarettes ?[{}]", hasCigarette);
if (hasCigarette) {
log.info(" You can start working ");
} else {
log.info(" Didn't do it ...");
}
}
}, " Xiaonan ").start();
new Thread(() -> {
synchronized (ROOM) {
Thread thread = Thread.currentThread();
log.info(" Did the takeout arrive ?[{}]", hasTakeout);
if (!hasTakeout) {
log.info(" No takeout , Take a break !");
try {
ROOM.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info(" Did the takeout arrive ?[{}]", hasTakeout);
if (hasTakeout) {
log.info(" You can start working ");
} else {
log.info(" Didn't do it ...");
}
}
}, " Little girl ").start();
TimeUnit.SECONDS.sleep(1);
new Thread(() -> {
synchronized (ROOM) {
hasTakeout = true;
log.info(" Here's the takeout !");
ROOM.notify();
}
}, " Take away delivery ").start();
}
In order to solve the false awakening , As a result, the correct thread is not awakened , We can try it notifyAll , In this way, the correct target thread must be awakened , But it will also wake up the threads that should not be awakened , So at this time , We can try it while Loop to determine whether it is an effective wake-up , Otherwise again wait
static final Object ROOM = new Object();
static boolean hasCigarette = false;
static boolean hasTakeout = false;
@SneakyThrows
public static void main(String[] args) {
new Thread(() -> {
synchronized (ROOM) {
log.info(" Do you have any cigarettes ?[{}]", hasCigarette);
while (!hasCigarette) {
log.info(" No smoke , Take a break !");
try {
ROOM.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info(" Do you have any cigarettes ?[{}]", hasCigarette);
if (hasCigarette) {
log.info(" You can start working ");
} else {
log.info(" Didn't do it ...");
}
}
}, " Xiaonan ").start();
new Thread(() -> {
synchronized (ROOM) {
log.info(" Did the takeout arrive ?[{}]", hasTakeout);
while (!hasTakeout) {
log.info(" No takeout , Take a break !");
try {
ROOM.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info(" Did the takeout arrive ?[{}]", hasTakeout);
if (hasTakeout) {
log.info(" You can start working ");
} else {
log.info(" Didn't do it ...");
}
}
}, " Little girl ").start();
TimeUnit.SECONDS.sleep(1);
new Thread(() -> {
synchronized (ROOM) {
hasTakeout = true;
log.info(" Here's the takeout !");
ROOM.notifyAll();
}
}, " Take away delivery ").start();
TimeUnit.SECONDS.sleep(1);
new Thread(() -> {
synchronized (ROOM) {
ROOM.notify();
hasCigarette = true;
log.info(" Here comes the smoke !");
}
}, " The cigarette man ").start();
}
So the correct use of templates should be
synchronized(lock) {
while( Conditions not established ) {
lock.wait();
}
// work
}
// Another thread
synchronized(lock) {
lock.notifyAll();
}
边栏推荐
- Leetcode316- remove duplicate letters - stack - greedy - string
- 数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化
- 手动实现一个简单的栈
- AcWing 886. Finding combinatorial number II (pretreatment factorial)
- [hardware ten treasures catalogue] - reprinted from "hardware 100000 whys" (under continuous update ~ ~)
- [hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply
- Global and Chinese markets of gps/gnss receiver modules 2022-2028: Research Report on technology, participants, trends, market size and share
- Like cloud functions
- [daily question in summer] first time, second time, deal!
- Global and Chinese market of paper machine systems 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
0xc000007b应用程序无法正常启动解决方案(亲测有效)
Neural networks - use sequential to build neural networks
Detailed explanation of distributed global unique ID solution
Data consistency between redis and database
Spanner 论文小结
Pytorch convolution operation
Distributed transactions - Solutions
解决:Thread 1:[<*>setValue:forUndefinedKey]:this class is not key value coding-compliant for the key *
Use and modification of prior network model
Neural network convolution layer
随机推荐
Detailed explanation of distributed global unique ID solution
Principle, technology and implementation scheme of data consistency in distributed database
FileInputStream
如何选择导电滑环材料
FileInputStream
AcWing 889. 01 sequence satisfying the condition (Cartland number)
Global and Chinese market of metal oxide semiconductor field effect transistors 2022-2028: Research Report on technology, participants, trends, market size and share
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Design experience of Meizhou clinical laboratory
Global and Chinese markets of superconductor 2022-2028: Research Report on technology, participants, trends, market size and share
AssertionError assert I.ndim == 4 and I.shape[1] == 3
Global and Chinese markets for business weather forecasting 2022-2028: Research Report on technology, participants, trends, market size and share
How to use common datasets in pytorch
Some tools that research dogs may need
AcWing 885. Find the combination number I (recursive preprocessing)
Is there any good website or software for learning programming? [introduction to programming]?
Pytoch (III) -- function optimization
Serialization and deserialization of objects
解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
每日一题-LeetCode1175-质数排列-数学