Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is wrong with this code?  RSS feed

 
Amer Seifeddine
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am thinking of writing a code which synchronizes put and leaves get non synchronized except when put executes :

Thanks!
[ May 04, 2008: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your post above. Please use these in the future to make your code readable.

This is a bad idea, I'm afraid. This will not, cannot prevent a "get" thread from accessing the map at the same time as a "put" thread. Well, it may prevent it, sometimes, but it won't work reliably in general. First, different threads can see inconsistent values of your "put" variable. Let's say you make it volatile to remove this problem. There are still things that can go wrong here. Here's one possible scenario:

1. Thread A begins a get(), and checks the value of variable "put", which is false. Go to else clause, then...

2. Thread B begins a put(), checks the value of variable "put", which is false. Thread B enters sync block, sets "put" to true, and begins put() operation.

3. Thread A resumes where it left off, beginning get() operation() (while thread B is also in put())

The problem is that you can check the value of "put" at one instant, but it can change an instant later. You can't guarantee that the state of the variable remains the same while you complete the get() operation, unless you put the get() operation in a sync block just like the put() operation is.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you (probably) want to accomplish can be done using a ReadWriteLock.
 
Amer Seifeddine
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies..Will I achieve what i need with high performance using below code then?

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before seeing the replies, I was thinking of an approach based on semaphores.But it has a restriction that you need to know the maximum number of allowed readers at a time. Something similar to below:



Will this work?
[ May 04, 2008: Message edited by: Satya Maheshwari ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amer: yes, that looks good. The only thing I would change is that I would put all the lock statements before the try. The unlock() in finally is something you only need to do (or should do) if the lock() statement did not throw an exception. So I'd write the methods like this:

Other differences are a matter of style, really. But I do think it's a good practice to get in the habit of starting a try/finally block immediately after the thing you need to undo in the finally block. E.g. you undo a lock() with an unlock().

Satya: I believe that will work, yes. Though it would be good to use try/finally there too, to ensure that the semaphore is released after it's acquired. Also it seems a bit artificial to put a limit on the number concurrent readers which are possible. I see you set fairness to true, which seems a good idea here to prevent lockout. I would guess, however, that using a ReadWriteLock will be at least as fast, and will communicate the programmer's intent here better.

For any of these strategies, it's a good idea to measure performance under realistic conditions with and without the sync/locking/semaphore stuff, to see if it makes a difference. In many cases the difference will be trivial anyway. And if it does make a big difference, you really need to measure to discover which technique will work best.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amer seif el dine:
[QB]Thanks for your replies..Will I achieve what i need with high performance using below code then?


If the base class IXRecord is/uses a HashMap then i think you will be better off using a ConcurrentHashMap.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!