This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

skipping synchronization on assuption of JAVA atomic primitive assignment.

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted following code in thread safe manner. So, In order to optimize I synchronized part of the method
Instead of entire method. Because part of the code occurs for every 10 mins ( intial un-synchronized will be called every-time)

Part of the method which is synchronized gets database updated value, and assigns it to member variable enabled.
This part of synchronizing code occurs for every 10 mins .

I didn't see any harm in doing this way unless JAVA primitive assignment is atomic.

Is java primitive assignment is atomic.
Also, please comment on my speculation on not making entire method synchronization.

Following is the code,

 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Boolean variable assignments are atomic.... but... non-volatile variables may be cached, hence, it is possible for threads to see previous values. You should declare the variable volatile.

Henry
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for taking time in replying me.

So, If I make it as volatile. Will the above method would be thread safe?

It also seems we need to take consideration boolean variable access atomicity.
(writing happening when reading in progress)
Also please let me know If you see any other synchronization issues

once again,

instead of


 
Master Rancher
Posts: 4755
71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of those two code examples, I think you're much better off with the second one, synchronizing the whole getEnabled() method. Imagine what happens if two threads call getEnabled() just past 10 minutes since the last DB refresh.

With the first code (using synchronized(this)): Thread A will check the time, see it's past 10 minutes, enter the synchronized(this) block, and do a DB update. Thread B will check the time, try to enter the synch block, block until thread A completes its update, and then... thread B will do another DB update, even though thread A just completed such an update.

With the second code (using a synchronized method): Thread A will enter the sync method, check the time, see it's past 10 minutes, and do a DB update. Thread B will try to enter the sync method, block until thread A completes its work, then enter, check the time, and skip the DB update because it can easily use the value of enabled set by thread A much less than 10 minutes ago.

The DB update is by far the slowest thing in all this code, and repeating it is silly. Thus, I recommend the second code over the first, definitely.

However, a better solution might be to just create a ScheduledExecutorService (or a java.util.Timer) to, in a separate thread from everything else, update the enabled field every 10 minutes. And make enabled volatile. That way the other threads will never have to wait for a DB update. It will just be done automatically every 10 minutes. A thread that checks enabled just before a refresh will see the old value, and one that checks after that will see the new value.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Imagine what happens if two threads call getEnabled() just past 10 minutes since the last DB refresh.


The simple solution for this could be an additional check in the synchronized block:


Some remarks: LASTDBACCESSTIME must be volatile too (if you implemented it as variable)
 
reply
    Bookmark Topic Watch Topic
  • New Topic