[concurrency-interest] Lock memory/resource Cost

Thierry Hanot thanot at infovista.com
Mon Oct 31 03:57:28 EST 2005



Hello 

I 'm just wondering what is the memory/system resource cost of
ReentrantLocks or read write lock.

I'm trying to use the concurrent lock when it's possible but there is one
case where I 'm not sure.

I have millions of object in memory and I need to synchronize access to each
one. I cannot use a global lock because the treatment is done by many
threads. So the simple way is to use classic java synchronization.
.....
synchronized(theObject){
	// treatment on theObject 
}
.....


But I want to use 
.....
theObject.getLock().lock();
try{
  // treatment on theObject
}finally{
	theObject.getLock().unlock();
}



-Because it has better performances.
-Because it's more flexible 
	- If I have something to do on many objects
		If I use classic java synchronization 

		List<MyObject> toDo = ...;
		For(MyObject theObject:toDo){
			synchronized(theObject){
				// treatment on theObject 
			}
		}

	

	
		If I use concurrent objects I could do something like
		
		Int cursor_ = 0;
		While(todo.size()>0){
			MyObject theObject = toDo.get(cursor_);
			If(theObject.getLock().tryLock()){
				Try{
					toDo.remove(cursor_);
					// treatment on theObject
				}finally{
					theObject.getLock().unlock();
				}	
			}
			if(toDo.size>0)
			cursor_ = (cursor_+1)%todo.size(); 
		}

But you need to have one lock per objects ( and I  really have millions ...)

So my question is:
 If I put a Lock by object.
	How much system resources will be used (is system resource are
acquired only when something has been done on the lock or when constructing
the object)

	What is the lighter lock implementation in memory?



Thank in advance for any recommendations.

T.Hanot


More information about the Concurrency-interest mailing list