In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "false awakening in Java multithreading and how to avoid it". In daily operation, I believe that many people have doubts about false awakening in Java multithreading and how to avoid problems. The editor consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "false awakening in Java multithreading and how to avoid it." Next, please follow the editor to study!
Let's take a look at an example.
A noodle shop with a noodle cook and a noodle eater needs to make sure that the cook makes a bowl of noodles and diners eat a bowl of noodles, not to mention eating noodles when there are no noodles at a time; according to the above operation, carry out ten rounds of noodle making and eating noodles.
Speak in code
First of all, we need to have a resource class, which contains the number of noodles, noodle operations, noodle eating operations.
When the number of noodles is 0, the cook will make the noodles and finish the noodles, and the waiting diners need to be awakened, otherwise the chefs will have to wait for the diners to finish the noodles before making noodles.
When the number of noodles is not zero, diners can eat noodles. After eating noodles, they need to wake up the waiting chef, otherwise diners need to wait for the chef to finish cooking noodles before eating noodles.
Then in the main class, we create a cook thread to cook noodles 10 times, and a diner thread to eat noodles 10 times.
The code is as follows:
Package com.duoxiancheng.code;/** * @ user: code essay * / class Noodles {/ / the number of noodles private int num = 0; / / Noodle method public synchronized void makeNoodles () throws InterruptedException {/ / if the number of noodles is not 0, wait for the diner to finish the noodles before making noodles if (num! = 0) {this.wait ();} num++ System.out.println (Thread.currentThread (). GetName () + "has made a noodle, currently has" + num+ "noodles"); / / after the noodles are ready, wake up the diners to eat this.notifyAll () } / / Noodle eating method public synchronized void eatNoodles () throws InterruptedException {/ / if the number of noodles is 0, wait for the chef to finish cooking noodles and then eat noodles if (num = = 0) {this.wait ();} num--; System.out.println (Thread.currentThread (). GetName () + "have eaten a noodle, there is currently" + noodle ") / / wake up the chef to make noodles this.notifyAll ();}} public class Test {public static void main (String [] args) {Noodles noodles = new Noodles (); new Thread (new Runnable () {@ Override public void run () {try {for (int I = 0; I)
< 10 ; i++) { noodles.makeNoodles(); } } catch (InterruptedException e) { e.printStackTrace(); } } },"厨师A").start(); new Thread(new Runnable(){ @Override public void run() { try { for (int i = 0; i < 10 ; i++) { noodles.eatNoodles(); } } catch (InterruptedException e) { e.printStackTrace(); } } },"食客甲").start(); }} 输出如下:It can be seen that it is output alternately.
What if there are two cooks, two diners, all doing 10 cycles?
The code of the Noodles class does not need to be moved. Just create two more threads in the main class. The code of the main class is as follows:
Public class Test {public static void main (String [] args) {Noodles noodles = new Noodles (); new Thread (new Runnable () {@ Override public void run () {try {for (int I = 0; I)
< 10 ; i++) { noodles.makeNoodles(); } } catch (InterruptedException e) { e.printStackTrace(); } } },"厨师A").start(); new Thread(new Runnable(){ @Override public void run() { try { for (int i = 0; i < 10 ; i++) { noodles.makeNoodles(); } } catch (InterruptedException e) { e.printStackTrace(); } } },"厨师B").start(); new Thread(new Runnable(){ @Override public void run() { try { for (int i = 0; i < 10 ; i++) { noodles.eatNoodles(); } } catch (InterruptedException e) { e.printStackTrace(); } } },"食客甲").start(); new Thread(new Runnable(){ @Override public void run() { try { for (int i = 0; i < 10 ; i++) { noodles.eatNoodles(); } } catch (InterruptedException e) { e.printStackTrace(); } } },"食客乙").start(); }} 此时输出如下: 虚假唤醒 上面的问题就是"虚假唤醒"。 当我们只有一个厨师一个食客时,只能是厨师做面或者食客吃面,并没有其他情况; 但是当有两个厨师,两个食客时,就会出现下面的问题: 初始状态 厨师A得到操作权,发现面的数量为0,可以做面,面的份数+1,然后唤醒所有线程; 厨师B得到操作权,发现面的数量为1,不可以做面,执行wait操作; 厨师A得到操作权,发现面的数量为1,不可以做面,执行wait操作; 食客甲得到操作权,发现面的数量为1,可以吃面,吃完面后面的数量-1,并唤醒所有线程; 6. 此时厨师A得到操作权了,因为是从刚才阻塞的地方继续运行,就不用再判断面的数量是否为0了,所以直接面的数量+1,并唤醒其他线程; 7. 此时厨师B得到操作权了,因为是从刚才阻塞的地方继续运行,就不用再判断面的数量是否为0了,所以直接面的数量+1,并唤醒其他线程;This is false awakening, and there are other situations in which readers can try to draw and analyze.
Solution method
The reason for false awakening is that there is no judgment from the blocking state to the ready state and then to the running state. We just need to let it make a judgment every time it gets the operation right.
So put
If (num! = 0) {this.wait ();}
Change to
While (num! = 0) {this.wait ();}
Set
If (num = = 0) {this.wait ();}
Change to
While (num = = 0) {this.wait ();} at this point, the study on "false awakening in Java multithreading and how to avoid it" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.