• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Developing a queuing mechanism

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two ideas to support single producer and multiple consumers.

The objective is that whichever consumer gets the data, processes it ( removes the element) and other consumers pick from next available element.

There are the options that I have now:
1. Create a consumer interface. Have all the consumers register themselves to the queue. Use observer pattern to propagate message availability signaled by producer.
2. Create a consumer interface, all consumers register themselves to the queue.  Use a thread pool with task to dequeue elements. All threads in the pool will take turn to dequeue elements, each thread representing a consumer.

There is one contention in the dequeue method, I am using lock which will be used by only one thread at a time. If I have to speed up the process, I will have to think of bringing real concurrency to this process.

I am engaged is some other task right now. But I will get on with trying both these approaches once I am free.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Updated the Main class to support multiple consumers.

Here is the idea I have right now:

1. I will have a list of consumers.
2. I will pass this list to EventHandler.
3. Inside EventHandler, I will created thread pool equal to the size of consumer list.
4. Whenever eventhandler finds an elements in the queue, next available consumer thread will pass this element to the consumer.
5. I will update the next available consumer reference.

Still working out the details of point 4, 5.

Here is the Main with the consumer list:
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Added class that will dispatch the event to consumer:


I just need to use a worker pool to connect this dispatcher to queue polling.

Here is an incomplete version of EventHandler with dispatcher:

 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some more update :




I am thinking of making list of available dispatchers. Once they are processing an event, I will remove them from this list. Once the task is done, I will add then again.

Does this look logically correct?
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Last piece of puzzle:

EventHandler.java


Main.java



This is my design with a single producer and multiple consumer criterea.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correction in EventHandler.java

 
please buy my thing and then I'll have more money:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic