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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:35:39+00:00 2026-06-12T19:35:39+00:00

A library I’m writing has a class implementing a two dimensional map and also

  • 0

A library I’m writing has a class implementing a two dimensional map and also provides, for efficient reading, smaller maps for the row/column views. So far all methods have been overridden so that any change in the main map are mirrored in the sub maps and vice versa. The problem comes in concurrent operation.

Ideally a removal of an item from the master map will simultaneously remove the item from the respective row and column maps, but this is of course impossible. For example in my put function:

public synchronized Cell put(Duple<Integer, Integer> key, Cell arg1){
    //preprocessing, detecting the row/col, creating row/col if not present yet, etc.
    Cell outcell = super.put(key, arg1);
    rowArr.putPriv(key.getElem2(), arg1);
    colArr.putPriv(key.getElem1(), arg1);
    arg1.assignCell(this, key);
    return outCell;
}

While it is perfectly acceptable for simultaneous reads of the map and even concurrent modification is not an issue (except for the creation/removal of rows/columns which needs remove and put to be synchronized), but The 4 stages of modification (super.put, the row and column puts, and the cell location update) need to be atomic to ensure it’s not possible to read unmatched data.

What are my options? As far as I’ve found from my searching it’s not possible to create an atomic sequence of statements in Java, and synchronisation isn’t going to work unless I synchronise all my functions (which prevents concurrent reads, and I would need locks on multiple items). I know the principles of (but am not particularly practiced in) basic semaphore concepts, but don’t see any simple ways to make a lock-on-write semaphore without massive complexity, particularly if I don’t want to have massive waits for a writing slot. What additional options do I have?

Note: due to the project I’m on I cannot use derivative languages such as groovy but can only use standard Java 1.6u24, without 3rd party libraries.

  • 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-12T19:35:40+00:00Added an answer on June 12, 2026 at 7:35 pm

    I’d suggest you not synchronize on entire method put. But synchronize only on on specific cell. In code below, I’ll describe how to do that:

    class ConcurrMattr {
    
        private ConcurrentHashMap<Integer, Lock> locks = 
                        new ConcurrentHashMap<Integer, Lock>();
    
        public Cell put( CellCoords key, Cell arg1 ) {
            // get or create lock for specific cell (guarantee its uniqueness)
            Lock lock = this.locks.putIfAbsent( coords.hash % 64, new ReentrantLock() );
            // 64 threads may concurrently modify different cells of matrix
    
            try {
                // lock only specific cell
                lock.lock();
    
                // do all you need with cell
    
                return ...;
    
            } finally {
                // unlock cell
                lock.unlock();
            }
        }    
    }
    
    
    // Immutable class to represent cell coordinates
    class CellCoords {    
        public final int x;
        public final int y;
        public final int hash;
    
        public CellCoords( int x, int y ) {
            this.x = x;
            this.y = y;
            this.hash = this.calcHash();
        }
    
        private int calcHash() {
            int result = 31 + this.x;
            return 31 * result + this.y;
        }
    }
    

    So you may synchronize read/write methods on specific cell, while other part of matrix will be accessible to other threads.

    Look at javadoc for ConcurrentHashMap and for Lock

    P.S.
    You may notice, that CellCoords field hash has type int. To avoid growth of locks map size till 2^31, you have to limit range of hashes. For example: (coords.hash % 64) – allows only 64 concurrent threads to work with entire matrix.

    P.P.S. Might be interesting article for you: http://www.ibm.com/developerworks/java/library/j-jtp08223/

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

Sidebar

Related Questions

The library I'm using has class G and class S which inherits G. I
A library provides a class with virtual functions. Can this class be extended with
Trove library provides some fast primitive collections, eg Map , List , but it
Which library has the definition for the global new and the delete operator? Specifically
Qt library provides a classes to work with SOAP in qt components . Unfortunately,
What library I need to reference to use System.Web.Script.Serialization in Class Library ? System.Web
Enterprise Library Validation Application Block (VAB) integrates with ASP.NET and also with WCF. Is
Which library do you use for N-dimensional arrays? I use blitz++ at work and
Gibberish library provides a nice CBC algo... // In Jascascript GibberishAES.enc(Made with Gibberish\n, password);
New Library: XParsec This question has lead to a stream-and-type-independent, non-linear, extensible parsec implementation

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.