You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directmemory.apache.org by "Michael André Pearce (Commented JIRA)" <ji...@apache.org> on 2012/02/26 02:47:48 UTC

[jira] [Commented] (DIRECTMEMORY-74) Investigate solutions to improving concurrency.

    [ https://issues.apache.org/jira/browse/DIRECTMEMORY-74?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13216612#comment-13216612 ] 

Michael André Pearce commented on DIRECTMEMORY-74:
--------------------------------------------------

Can i suggest locking on the key for put/updates/deletes? avoids someone getting a key whilst it is in transitive state of being updated by another. But at the same time we dont want an array of thousands of locks as this will bloat memory and is an ugly solution.

Propose the following to get locks for a key, inspired on work done by doug lee and concurrenthashmap in java concurrent package.

A holder class that contains an array of locks (where lockId = lockarray index) and then use the following bit of doug lee black magic to select the lock to return, this allows for locking safely yet, keeping a spread of threads running concurrently. below is a pseudo example of how to get a lock for key, default number of locks is 32 as is same with concurrent hash map.


 public class LockHolder<K> {
 private final Lock[] locks;

 public LockHolder(){
   this(32);
 } 
 public LockHolder(int numberOfLocks){
    if ((numberOfLocks & numberOfLocks - 1) == 0){
    locks = new Lock[numberOfLocks];
	//populate array with locks;
    } else {
       throw new LockHolderExcpetion("number of lucks must be to the power of 2!");
    }
 }
 public Lock getLock(K key)
 {
   int lockId = hash(key) & numberOfLocks -1;
   return this.locks[lockId];
 }

/**
  * Return hash code for Object key. Since we are using power-of-two
  * tables, it is worth the effort to improve hashcode via
  * the same multiplicative scheme as used in IdentityHashMap.
  */
 protected static int hash(K key) {
   int h = x.hashCode();
   // Multiply by 127 (quickly, via shifts), and mix in some high
   // bits to help guard against bunching of codes that are
   // consecutive or equally spaced.
   return ((h << 7) - h + (h >>> 9) + (h >>> 17));
 }
}

                
> Investigate solutions to improving concurrency.
> -----------------------------------------------
>
>                 Key: DIRECTMEMORY-74
>                 URL: https://issues.apache.org/jira/browse/DIRECTMEMORY-74
>             Project: Apache DirectMemory
>          Issue Type: Task
>            Reporter: Michael André Pearce
>


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira