Producer Consumer Pattern Using a BlockingQueue in Java

preview_player
Показать описание
Producer-Consumer Pattern using BlockingQueue
:



Рекомендации по теме
Комментарии
Автор

you help me lot !eventually,i found the answer that why my data can not printed enough, answer is so simple in word "put".

prometheusli
Автор

Thanks for the video, I found it useful. I'm assuming you meant "questionNo" to be a static variable or something? Otherwise, it is just a member variable to each instance of Producer class and incrementing it without synchronization is totally fine. However, if it was declared static or shared among threads, then mutating it must be synchronized. AtomicInteger could be a good alternative. Also nested synchronization blocks (locking on the same instance) is ok as the lock is reentrant - I am confident that the synchronized block implementation has been highly optimized and you will see virtually zero performance hit from reentering a synchronized block that the current thread already owns. Mainly leaving this for future visitors but please correct me if I'm missing something. Cheers.

BILSON