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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:49:49+00:00 2026-05-26T03:49:49+00:00

If I want to lock the whole array I can use the synchronized keyword

  • 0

If I want to lock the whole array I can use the synchronized keyword like this:

int arr[];

synchronized void inc(int a, int b){
    arr[a]=arr[a]+b;
}

But can I lock just the item arr[a] so that other threads can still read/write other items of the array at the same time?

  • 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-26T03:49:50+00:00Added an answer on May 26, 2026 at 3:49 am

    Not out-of-the-box, but you can create an Object array that is the same size as your int array and populate the array with distinct Objects. So when you want to lock on a particular element in the int array, you lock on that Object at the corresponsible index:

    final Object[] locks = new Object[arr.length]:
    for(int i = 0; i < arr.length; i++) {
     locks[i] = new Object();
    }
    

    When locking: do

    synchronized(locks[a]) {
      // do something here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to lock back button while my data process. I use AsyncTask to
I have a database and want to lock it ... I know I can
Just want to start out by saying this seems like a great site, hope
I want to lock specific keys/whole keypad programmatically in Windows Phone 7. Is there
I'd like to use a map that would be equivalent to ConcurrentMap (I want
I have a UIScrollView with a UIView inside. I want to lock the x-axis
I'm using SqlCipher with content providers. Right now, when I want to lock the
I want to add explicit lock on row which is currently being updated and
I want to permanently disable the lock in Android screen. i.e even after power
I have implemented ReaderWriterLockSlim, Now i don't want it to wait at the lock.

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.