Java Producer Consumer concurrent collaboration

Source: Internet
Author: User

With the job change, code wasted a long time, many times are in communication needs, as a technician, do not write code on the feeling is in the self-waste martial arts, slowly decadent a lot, today re-review the next Java threading Knowledge, the basic knowledge is not combed, the internet is also a lot, the main key several state bits (new, can run, Is running, blocking, etc.) and several key methods (sleep, yield, wait, notify, notifyall, etc.) to make clear, basic application knowledge should be familiar with, want to learn more about the recommendation of "Java concurrent programming combat." Not much nonsense to say, directly on the example, looked at the general idea of the Internet, the actual application of the scene, wrote an example of their own, not in place please correct me, thank you!

Example: Classic producers and consumers, multi-threaded environment collaborative production and consumption: multiple producers at the same time to perform production tasks, multiple consumers at the same time to carry out consumer tasks, when the product is full, the producers wait, will lock to consumers, consumers start spending, when no product can be consumed, consumers wait, The lock is given to the producer and the producer begins production. Define the total production cap max_produce and the maximum capacity per producer (upper limit) max_signle_produce and the maximum amount of consumption per consumer max_single_consume, in particular: if the total production after a single production is not up to the limit , the locks produced by producers at this time may be acquired by consumers for consumption, and may also be acquired by producers to continue production, and the same is true of consumers. Because in a multithreaded environment, between all production lines, between consumer threads, and between the production line and the consumer thread, it is possible to observe that all products are produced by one thread or consumed by one thread, and the run results are saved to a file for a slightly longer run time, It is possible to observe the situation of multiple producers concurrently producing products or multiple consumer concurrent consumption products.

Operation Result:

Java Producer Consumer concurrent collaboration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.