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 8507907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:00:11+00:00 2026-06-11T03:00:11+00:00

Suppose that there are many threads that call the method m(int i) and change

  • 0

Suppose that there are many threads that call the method m(int i) and change the value of the array in position i. Is the following code correct, or is there a race condition?

public class A{
    private int []a =new int[N];
    private Semaphore[] s=new Semaphore[N];

    public A(){
        for(int i =0 ; i<N ; i++)
           s[i]=new Semaphore(1);
    }

    public void m(int i){
        s[i].acquire();
        a[i]++;
        s[i].release();
    }
}
  • 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-11T03:00:12+00:00Added an answer on June 11, 2026 at 3:00 am

    The code is correct, I see no race condition although both a and s should be made final. You should also use a try/finally every time you use locks that need to be acquired and released:

    s[i].acquire();
    try {
       a[i]++;
    } finally {
       s[i].release();
    }
    

    But, for updating an array, the idea of individual locks per item is very unnecessary. A single lock would be just as appropriate since the major cost is the memory updating and the other native synchronization. This said, if the actual operation is not a int ++ then you are warranted in using a Semaphore or other Lock object.

    But for simple operations, something like the following is fine:

    // make sure it is final if you are synchronizing on it
    private final int[] a = new int[N];
    ...
    
    public void m(int i) {
       synchronized (a) {
          a[i]++:
       }
    }
    

    If you are really worried about the blocking then an array of AtomicInteger is another possibility but even this feels like overkill unless a profiler tells you otherwise.

    private final AtomicInteger[] a = new AtomicInteger[N];
    ...
    
    public A(){
        for(int i = 0; i < N; i++)
           a[i] = new AtomicInteger(0);
    }
    
    public void m(int i) {
        a[i].incrementAndGet();
    }
    

    Edit:

    I just wrote a quick stupid test program that compares a single synchronized lock, a synchronized on an array of locks, AtomicInteger array, and Semaphore array. Here are the results:

    • synchronized on the int[] 10617ms
    • synchronized on an array of Object[] 1827ms
    • AtomicInteger array 1414ms
    • Semaphore array 3211ms

    But, the kicker is that this is with 10 threads each doing 10 million iterations. Sure it is faster but unless you are truly doing millions of iterations, you won’t see any noticeable performance improvement in your application. This is the definition of “premature optimization”. You will be paying for code complexity, increasing the likelihood of bugs, adding debugging time, increasing maintenance costs, etc.. To quote Knuth:

    We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

    Now, as the OP implies in comments, the i++ is not the real operation that s/he is protecting. If the increment is a lot more time consuming (i.e. if the blocking is increased), then the array of locks will be required.

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

Sidebar

Related Questions

Suppose that there're two types of packet in the chatting server. Those two packets
Suppose there's a top-secret list (inherits its permissions from its parent) that contains records
Suppose i have one folder and inside that another two files are there .
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
There is a following function that is supposed to make a comparison between 2
Suppose that I have used a free() function to free a memory that,for many
I appreciate that there are now many mechanisms in dotnet to deal with XML
Is it possible to do this: suppose that there are a lot of same
Suppose that we have previously instantiated three objects A, B, C from class D
Suppose that I have n points (x,y1),(x2,y2),.....(xn,yn) my aim is to connect these points

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.