Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronisation in singleton class with common resource

 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?



 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Praveen Kumar Singh
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic