Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Java multithreaded Program

Shulou Source: shulou.com Published: 2022-06-02 12:40:23 09月30日 Update

Today, I will talk to you about the example analysis of Java multithreaded programs, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

We encounter a lot of problems when using Java multithreaded programs, and when we solve this problem, it is the source of the answer in the source code. Let's take a look at the source code of the Java multithreaded program.

For Java multithreaded programs, the producer and consumer models are the most classic, regardless of any programming language. Just like learning every programming language, Hello World! Are the most classic examples. In fact, it should be the "producer-consumer-warehousing" model, without warehousing, the producer-consumer model appears to be unconvincing. For this model, the following points should be made clear:

1. The producer only produces when the warehouse is not full, while the warehouse is full and stops production.

2. Consumers can consume only when there is a product in the warehouse, while the warehouse is empty.

3. When consumers find that there are no products to consume in storage, they will notify the producers to produce them.

4. When producing consumable products, producers should inform waiting consumers to spend.

This model will combine java.lang.Object 's wait with notify and notifyAll methods to achieve the above requirements. This is very important.

Java multithreaded program code

Public class Test {public static void main (String [] args) {Godown godown = new Godown (30); Consumer C1 = new Consumer (50, godown); Consumer c2 = new Consumer (20, godown); Consumer c3 = new Consumer (30, godown); Producer p1 = new Producer (10, godown); Producer p2 = new Producer (10, godown); Producer p3 = new Producer (10, godown); Producer p4 = new Producer (10, godown); Producer p5 = new Producer (10, godown) Producer p6 = new Producer (10, godown); Producer p7 = new Producer (80, godown); c1.start (); c2.start (); c3.start (); p1.start (); p2.start (); p3.start (); p4.start (); p5.start (); p6.start (); p7.start ();}} after reading the above, do you have any further understanding of the example analysis of Java multithreaded programs? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Consumption production programs threads time consumers producers models warehousing products content examples analysis source code classics programming languages languages problems programming important Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information Huawei Shulou Tech Info Shulou Technology