Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:36:07+00:00 2026-06-07T21:36:07+00:00

I have a data var, monthArray , that is read by multiple consumers and

  • 0

I have a data var, monthArray, that is read by multiple consumers and periodically updataed by a single periodicall scheduled updater thread. All asynchronously.

I have considered both of these options to perform the update safely.

    ArrayList<String> tempArray = ModelJob.getDistinctMonths(user, true);       
    synchronized (monthArray) {
        monthArray = tempArray;
    }

or

    synchronized (monthArray) {
        monthArray = ModelJob.getDistinctMonths(user, true);
    }

The idea behind the first one being that the ModelJob.getDistinctMonths(user, true); call is time consuming and I dont want to hold the synchrinzation to block for longer then it has to, only for the quick reassignment of the old array with the updated array. However seems to be a obfuscated, and I only want to do it if completly nessecary. Can anyone give me any insight into how the jvm handles this synchrization and weather or not doing the former will give me any increase in performanace? Basically Im asking if the jvm would block for the whole static ModelJob call, or if it is able to get away with only blocking for the reassignment and be safe, and if so, if it is smart enought to do so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:36:10+00:00Added an answer on June 7, 2026 at 9:36 pm

    assuming that you don’t need the synchronization around the getDistinctMonths() call (that call is thread-safe, and you don’t need atomicity around the call and the assignment), then you can just synchronize around the assignment (and yes, the blocking is only scoped to the synchronized block, otherwise the syntax would be pointless). Note, @JohnVint brings up a good point that you should not synchronize on the monthArray reference as it is being modified. you must synchronize on a separate object instance which does not change.

    Lastly, you could remove the synchronization block and make the monthArray member volatile and achieve the same results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have JSON/JS like this that populates data: var settingDefs = []; settingDefs.push({ name:'responses',
I have data stored in a SQL database that I'm attempting to read into
I have this code that populates the div with the form input data: var
I have data like the following: var data = [{ id: 1, date: new
I have data represented in CF as an array of structs e.g.: var foo
I have this JavaScript code: for (var idx in data) { var row =
I have the following: somefile.php $(document).ready(function() { var handler = function(data) { var JsDiv
I have a function which is like $(function() { var data = [ {
I have a piece of javascript code as follows: var data = { ...
If I have HTML content in a variable like so: var data = <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.