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

  • Home
  • SEARCH
  • 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 222725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:09:15+00:00 2026-05-11T19:09:15+00:00

I am wondering if it is possible to avoid the lost update problem, where

  • 0

I am wondering if it is possible to avoid the lost update problem, where multiple threads are updating the same date, while avoiding using synchronized(x) { }.

I will be doing numerous adds and increments:

val++;
ary[x] += y;
ary[z]++;

I do not know how Java will compile these into byte code and if a thread could be interrupted in the middle of one of these statements blocks of byte code. In other words are those statements thread safe?

Also, I know that the Vector class is synchronized, but I am not sure what that means. Will the following code be thread safe in that the value at position i will not change between the vec.get(i) and vec.set(...).

class myClass {
  Vector<Integer> vec = new Vector<>(Integer);

  public void someMethod() {
    for (int i=0; i < vec.size(); i++)
      vec.set(i, vec.get(i) + value);
  }
}

Thanks in advance.

  • 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-05-11T19:09:15+00:00Added an answer on May 11, 2026 at 7:09 pm

    For the purposes of threading, ++ and += are treated as two operations (four for double and long). So updates can clobber one another. Not just be one, but a scheduler acting at the wrong moment could wipe out milliseconds of updates.

    java.util.concurrent.atomic is your friend.

    Your code can be made safe, assuming you don’t mind each element updating individually and you don’t change the size(!), as:

    for (int i=0; i < vec.size(); i++) {
        synchronized (vec) {
            vec.set(i, vec.get(i) + value);
        }
    }
    

    If you want to add resizing to the Vector you’ll need to move the synchronized statement outside of the for loop, and you might as well just use plain new ArrayList. There isn’t actually a great deal of use for a synchronised list.

    But you could use AtomicIntegerArray:

    private final AtomicIntegerArray ints = new AtomicIntegerArray(KNOWN_SIZE);
    [...]
        int len = ints.length();
        for (int i=0; i<len; ++i) {
            ints.addAndGet(i, value);
        }
    }
    

    That has the advantage of no locks(!) and no boxing. The implementation is quite fun too, and you would need to understand it do more complex update (random number generators, for instance).

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

Sidebar

Related Questions

I'm wondering if it is an accepted practice or not to avoid multiple calls
I am wondering if it is possible to post data without using the standard
Was just wondering if any of you already coded a http get request using
Wondering if this is possible: Let's say if I have a text input element
I was wondering if it possible to access iPhone User Music, Videos, Photos and
I'm wondering if its possible to resize flash with javascript. I can't change any
I'm wondering if its possible to do this. The issue being that im trying
I was wondering is it possible to prevent only a one element in plot
I was wondering if it is possible (I'm sure it is) to get a
I am wondering what is the best way to support audio/video chat on a

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.