jeudi 6 juin 2019

Should blocking queue be defined in Producer Class or Consumer class?

I have made 2 classes of Producer and Consumer. Producer class creates threads and pushes the items to the blocking queue and consumer class also creates threads which takses the items from the blocking queue.

Now, I was wondering one thing here. Should I define my blocking queue in Producer or in Consumer class? Or it should be a singleton object which is defined in both producer and consumer?

If I define in only one class, then I can access the blocking queue by calling the method of that class from another class. This kind of encapsulates the blocking queue data structure in that class and we are accessing that data structure only by the use of the methods. But, I find calling through method makes it look like, one class is dependent on another class.

While if I create a singleton blocking queue, then, the blocking queue object is shared between 2 classes. But I think its bad practice to share collections between classes making users to know that how to use collections in each class. I am not sure if its really a bad practice.

Please provide your insights on which way is better? Thanks for your help.

Aucun commentaire:

Enregistrer un commentaire