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