This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Threads and Synchronization and the fly likes Synchronisation in singleton class with common resource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronisation in singleton class with common resource" Watch "Synchronisation in singleton class with common resource" New topic
Author

Synchronisation in singleton class with common resource

Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
Hi, i have a situation like below code.
what i am doing here is populating the products on start up and the getProduct method iterate over list and get the suitable product.
My doubt is, do i need to make this synchronized ?
classical problem is, lets say T1 is iterating over products and T2 enter in clearProduct->populateProducts and flush all the products while T1 is reading it.
if i have to, then won't system will very slow?
Does someone has better solution ?





Praveen
SCJP, SCWCD, SOA
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi,

Yes. If one thread is iterating over a list, and another is removing items from list, then there must be synchronization.

Praveen Kumar Singh wrote:then won't system will very slow?

Well, its not 'very' slow. Besides, do you want a slightly slow and working system, or fast and non-working system?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Does someone has better solution ?

A better solution depends on use-case. If the data is regularly written (and read) from the list then you might take a look at one of the collections in the java.util.concurrent package. There are several of them there, an which one to use depends on your use-case (how do you navigate the collection, what is the expected ratio of reads to writes...)

But you also say:
what i am doing here is populating the products on start up and the getProduct method iterate over list and get the suitable product.


If you know the code for writing to the product list only occurs at startup then you might be best off using a CountDownLatch to signal the list is filled. Start with a CountDownLatch with a single count. Then when you are at the end of the populateProducts method you would countDown(), bringing the count down to 0. When you call any of the getters, you await() on the CountDownLatch so that all threads that need to get the products wait until the products are populated.


Steve
Praveen Kumar Singh
Ranch Hand

Joined: Mar 04, 2009
Posts: 43
Thanks Steve and Anayonkar
But, i am still not convince, it has to be some better, simple and straight forward solution.
Lets understand the situation one more time.
The chances that i will flush is very rare, may be once or twice in a month.
But, thread which will access the will be more than 1000 in a minute.
Having synchronization, still seems a costly deal!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

1000/min seems like it isn't a high stress situation... Still, a CopyOnWriteArrayList seems like it would work in your case. And if you feel the need to do your own locking then using a ReadWriteLock where each of your access methods grab the Read part of the lock will also prevent overlapping reads from affecting each other. And the rare clearing would use the Write part of the lock - waiting till all Reads are done, then blocking more Reads until the Write is done.
 
wood burning stoves
 
subject: Synchronisation in singleton class with common resource