In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the two ways of Java to achieve producers and consumers, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
When I went to interview programmers eight years ago, a small company where the development director received us, the topic for me was to write a program to simulate producer-consumer problems, which made me so difficult at that time. I suddenly feel that my knowledge reserve is so scarce.
After I work in DBA, there are often a large number of concurrent environments, some need to be eliminated, some need to be optimized. In many concurrent scenarios, it is found that producer-consumer problems can simulate many practical problems, so producer-consumer problems are very important, and it is also a kind of problem that I want to constantly improve and explore.
The necessity of introducing warehouses
In fact, it doesn't take too much time to use the program to simulate. Let's briefly talk about the areas that need to be considered. First of all, producers and consumers are two physical objects, producers produce goods and consumers consume goods. if the production process is defined in the producer and the consumption process is defined in the consumer, the two objects need to refer to each other, and the dependence is too high. and in fact, the performance is not much better, so we need a buffer, an intermediate object, we call it the warehouse, and the goods produced are pushed into the warehouse. The consumed goods are removed from the warehouse so that producers and consumers can cancel the reference between them and synchronize the state directly through the warehouse reference and reduce the coupling.
So one of our preliminary ideas is producer-> warehouse MAX_SIZE)
{
System.out.println (Thread.currentThread (). GetName () + "[producer: quantity of products to be produced]:" + num + "\ t [inventory]:"
+ list.size () + "\ t cannot perform production tasks temporarily!")
Try
{
/ / production is blocked because the conditions are not met
List.wait ()
}
Catch (InterruptedException e)
{
E.printStackTrace ()
}
}
/ / produce num products when the production conditions are met
For (int I = 1; I
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.