This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

implementing producer consumer with singleton

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am new to the concept of singleton. I have a text file and whenever i update this file, producer should be notified and many consumers say 5 threads try to access this. Only one thread should be able to read this file and that should write to the another text file. When second thread tries to access the same file at the same time, it should be said that thread 1 is accessing.


Please help me on how to approach this kind of scenario.
It would be easy for me to understand if some one can code it.
Thanks in advance
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't understand your needs completely from your description and as such I'm not exactly sure the producer-consumer pattern ideally fits your needs. If this does not sound right, please come back to clarify.

Anyway, this pattern can be rather easily implemented in Java by using one of the synchronized queues in the java.util.concurrent package, such as ArrayBlockingQueue. All the hard synchronizing work is already implemented in these queue classes. You'll create one of these (read their javadoc and choose the one that fits your needs best). Then let producer threads put work items into that queue and consumer threads retrieve and process these items. This design ensures that only one consumer will start processing the work item (probably a file in your case) and therefore you don't need to further coordinate worker threads not to compete for work on a single file.

The queue could be part of a singleton, so that all threads share the same instance (this is naturally very important). Googling up singleton design pattern brings up many materials on this; try some of them, as conceptually singleton pattern belongs to the easier ones.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahana mithra wrote:I have a text file and whenever i update this file, producer should be notified


In producer-consumer model, producer notifies the consumers (i.e. consumers are notified, not the other way round).

Secondly, the behavior you want can be simply done by synchronization. I don't understand why and how you reached the conclusion that you need singleton to fix this thing.

I hope this helps.
 
Greenhorn
Posts: 1
Oracle Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using BlockingQueue from util.concurrent pkg for your producer consumer pattern.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic