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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:41:27+00:00 2026-05-25T11:41:27+00:00

I had a 2×2 array that I had two threads operating on. it is

  • 0

I had a 2×2 array that I had two threads operating on.

it is possible to use a synchronized statement in java on an array?

how does the locking work? the java tutorial thread said that the synchronized statement works on objects, so I wasn’t sure what they meant. Another site said that I could make a statement like

synchronized (array1[]){

}

Does this synchronize accesses to everything in the array so that the array is locked to other threads?

if I have a two-d Array can i use

synchronized (array1[i]) to lock one of the rows of the array?

and is it possible to lock individual array values
with something like

synchronized (array1[i][j]){

}

But yeah, tips or help are greatly appreciated. Actually I already turned it in, correct or not. But I want to know for future use

  • 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-25T11:41:28+00:00Added an answer on May 25, 2026 at 11:41 am

    Yes, you can synchronize using an array as monitor object, because arrays (even arrays of primitives) are objects in Java.

    You can synchronize a block of code on a specific monitor like this:

    public void myMethod() {
    
        unsynchronized_statements...
    
        synchronized(myMonitorObject) {
            synchronized_statments...
        }
    

    It is best practice to synchronize as few lines of code as possible.

    Synchronizing code on a monitor does not affect the monitor in any way, it only affects the threads accessing the synchronized block of code. Before thread execution can enter the block of code, it must obtain ‘the lock’ on the monitor. The Java runtime ensures that at most one thread at a time can have ‘the lock’ on a monitor. So synchronizing on your array does not prohibit unsynchronized blocks of code to access it! The trick is to make sure that all the operations you don’t want to happen at the same time are within blocks synchronized on the same monitor.

    Since Java does not offer multi-dimensional arrays, only arrays-of-arrays, you can certainly synchronize on a nested array for more fine-grained synchronization. If you model a 2d array as an array of rows, you can only synchronize on rows, not on columns because in that example columns are not represented as separate arrays.

    You can only synchronize on single array values if these are non-primitve, so Integer() instead of int. Note that Integer() is an immutable object, so you would not be able to change its value. A solution would be to create your own Cell() wrapper object with a getter and setter for the contained numeric value. This would allow you to let a thread get a lock on the Cell and safely change its value.

    Because it’s my day off I decided to have some fun and created a working example of what you describe. Yes, this is my idea of having fun.

    Classes:

    • Matrix : representation of a 2d matrix of cells
    • Cell : wrapper for a matrix cell value
    • Operation : An abstract operation on an array of Cells
    • IncrementOperation : an Operation which increments each Cell value
    • ReverseOperation : an Operation which reverses the order of the cells
    • Main : the application

    The application starts multiple operations on the same matrix. The only synchronized block of code is in the class Operation. If you remove the synchronization, the results will be wrong because two operations are manipulating the same row simultaneously.

    Output when synchronized:

    [105, 104, 103, 102, 101]
    [110, 109, 108, 107, 106]
    [115, 114, 113, 112, 111]
    [120, 119, 118, 117, 116]
    [125, 124, 123, 122, 121]
    [130, 129, 128, 127, 126]
    [135, 134, 133, 132, 131]
    [140, 139, 138, 137, 136]
    [145, 144, 143, 142, 141]
    [150, 149, 148, 147, 146]
    

    Example output when NOT synchronized:

    [105, 4, 103, 102, 101]
    [110, 9, 108, 207, 106]
    [115, 14, 113, 212, 111]
    [120, 19, 118, 217, 116]
    [125, 124, 123, 122, 121]
    [130, 129, 128, 127, 126]
    [135, 34, 133, 232, 131]
    [140, 139, 138, 137, 136]
    [145, 144, 143, 142, 141]
    [150, 149, 148, 147, 146]
    

    Note that I added some Thread.sleep() statements in the operation implementations to make the difference between synchronized and unsynchronized execution more obvious.

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

Sidebar

Related Questions

Had a page that was working fine. Only change I made was to add
Had a good search here but can't see anything that gets my mind in
I had a discussion with some colleagues mentioning that there are not too many
I had to delete all the rows from a log table that contained about
I had the idea of a search engine that would index web items like
Had a conversation about sitemaps with someone from marketing. It was stated that a
I had two apps in xcode, which i wanted to combine, so i started
Had a look over SO but I can't see any threads which address my
Had I looked into the Java SE6 documentation sooner on Context and InitialContext, I
Had a question that I've often wondered about. Is it better to have multiple

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.