File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes implementing producer consumer with singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "implementing producer consumer with singleton" Watch "implementing producer consumer with singleton" New topic
Author

implementing producer consumer with singleton

sahana mithra
Ranch Hand

Joined: Oct 26, 2010
Posts: 72
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
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3433
    
  47

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.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
claudiu cosar
Greenhorn

Joined: Jun 19, 2010
Posts: 1

Try using BlockingQueue from util.concurrent pkg for your producer consumer pattern.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: implementing producer consumer with singleton
 
Similar Threads
Please help me get started with this assignment
Use a singleton or a "purely static" class?
singleton synchronized doubt
Please help me get started with this assignment
Reflections on singleton and synchronized methods