Detailed introduction to multithreaded producer consumer problems in Java

Multi thread producer consumer problem in Java

preface:

In general, I like to ask some thread questions in the interview. The more basic questions are nothing more than deadlock, producer consumer problems, thread synchronization, etc. deadlock has been written in the previous article. Here is the question of more production and more consumption

This is a multi-threaded consumer producer problem in JDK version 1.5 or above. The optimization is to split the synchronized keyword step by step, separate the object monitor, synchronize, and implicitly establish a monitor. This can establish multiple monitors, and the wake-up is also optimized. Previously, if it was synchronized, notifyall(), When only consumers or producers need to be awakened, this notifyall() will awaken all frozen threads, resulting in a waste of resources. Here, only the opposite threads will be awakened. The explanation of the code is all in the source code and can be copied directly.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. I hope this article can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>