In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
In the previous article, the landlord explained the cause of the deadlock in multithreading and threw out the problem-the liberation scheme of deadlock, so in this article, the landlord will quote a KFC to produce hamburgers and the process of customers buying hamburgers to explain the deadlock solution and the multi-threaded wait and wake-up mechanism.
Simply use a picture to illustrate the process that KFC produces hamburgers and customers consume them:
Scenario analysis:
Resource class: Hamburger
Set hamburger data: SetThread (producer)
Get Hamburg data: GetThread (Consumer)
Test class: HamburgerTest
Operations of different kinds of threads (producers, consumers) on the same resource (Hamburg)
When the hamburger is in stock, the hamburger is no longer produced and the customer can consume; on the contrary, when the hamburger is produced, the customer cannot consume
Are there any thread safety issues? That's for sure. The landlord gives the way to judge in the article "Thread Safety issues", which is all satisfied in this scenario.
Code construction: the I attribute in the class is specially added by the landlord for good results, which has nothing to do with the problems to be explained in this article.
The first is the resource class Hamburger.java, where only three fields are simply constructed for simulation, in which flag is used to indicate whether the resource has data.
1 package com.jon.hamburger; 2 3 public class Hamburger {4 private String name;// Hamburg name 5 private double price;// Hamburg Price 6 private boolean flag;// Hamburg whether there is a data mark. The default is false, indicating that there is no data 7 public String getName () {8 return name; 9} 10 public void setName (String name) {11 this.name = name. 12} 13 public double getPrice () {14 return price;15} 16 public void setPrice (double price) {17 this.price = price;18} 19 public boolean isFlag () {20 return flag;21} 22 public void setFlag (boolean flag) {23 this.flag = flag;24} 25 26}
Then there are producers SetThread.java and GetThread.java, both of which need to implement the Runnable interface. Point 7 in the scenario analysis has shown that there is a thread safety problem in the scenario. The landlord explained in the previous article that the thread safety problem can be solved by adding locks, but different kinds of threads are involved here. Therefore, two points must be met:
Different kinds of threads need to be locked
Different kinds of threads must add the same lock.
SetThread.java
1 package com.jon.hamburger; 2 3 public class SetThread implements Runnable {4 private Hamburger hamburger; 5 private int i; 6 7 public SetThread (Hamburger hamburger) {8 this.hamburger = hamburger 9} 10 @ Override11 public void run () {12 while (true) {/ / for better data effect, the landlord added a judgment of 13 synchronized (hamburger) {14 if (this.hamburger.isFlag ()) {/ / if there is 15 try {16 hamburger.wait () / / Thread waits for 17} catch (InterruptedException e) {18 e.printStackTrace () 19} 20} 21 / / if there is no stock, this simulates the production of 22 if (I% 2 = = 0) {23 this.hamburger.setPrice (25.0); 24 this.hamburger.setName ("Junpan's hamburger") 25} else {26 this.hamburger.setPrice (26.0); 27 this.hamburger.setName ("Dajun pot's hamburger"); 28} 29 this.hamburger.setFlag (true); / / change the logo 30 hamburger.notify () after production is completed / / Wake up the currently waiting thread 31 iWake up the waiting thread / only for better data effect, no practical meaning 32} 33 34} 35 36} 37 38}
GetThread.java
1 package com.jon.hamburger; 2 3 public class GetThread implements Runnable {4 5 private Hamburger hamburger; 6 / * * 7 * in order for the synchronization lock to use the same object lock, 8 * @ param hamburger 9 * / 10 public GetThread (Hamburger hamburger) {11 this.hamburger = hamburger is passed through the construction method. 12} 13 @ Override14 public void run () {15 while (true) {16 synchronized (hamburger) {17 if (! this.hamburger.isFlag ()) {/ / if there is no stock, the thread waits for 18 try {19 hamburger.wait () 20} catch (InterruptedException e) {21 e.printStackTrace (); 22} 23} 24 / / if there is data, output 25 System.out.println (this.hamburger.getName () + "-" + this.hamburger.getPrice ()) 26 this.hamburger.setFlag (false); / / change flag 27 hamburger.notify (); / / Wake up thread 28} 29} 30 31} 32 33}
You can see that sysnchronized is used for locking in the run methods of both thread classes, and the same hamburger object lock is used.
Then take a look at the test class HamburgerTest.java and its output:
1 package com.jon.hamburger; 23 45 public class HamburgerTest {6 7 8 public static void main (String [] args) {9 Hamburger hamburger = new Hamburger (); 10 11 SetThread st = new SetThread (hamburger); / / passing shared resource data hamburger12 GetThread gt = new GetThread (hamburger); 13 14 Thread td1 = new Thread (st); 15 Thread td2 = new Thread (gt) 16 17 td1.start (); 18 td2.start (); 19 20} 21 22}
In the test class, we pass the same object to SetThread and GetThread through the constructor to ensure that the lock object is the same.
Output results, threads do not affect each other, and there is no NULL-0.0 output:
1 Junpan hamburger-25.02 Dajun pot hamburger-26.03 Junpan hamburger-25.04 Dajun pot hamburger-26.05 Junpan hamburger-25.06 Dajun pot hamburger-26.07 Junpan hamburger-25.08 Dajun pot hamburger-26.0
Code analysis:
Assuming that thread T2 first grabs the right to execute CPU, then the program execution process can be shown in the following figure:
According to the program code analysis, it can also be seen that the deadlock problem caused by waiting for each other between threads can also be solved, and the solution is through wake-up. In addition, the text owner also used another way, the idea is similar, the sample code is put together with the sample code of this article, and has been uploaded to GitHub.
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.