ReentrantReadWriteLock understanding

Hi,

I am studying the mechanism of readLock() and writeLock() but is still hazy on them even read through the java.util.concurrent.locks API doc many times. I have encountered the following questions:

1. As I know the writeLock() is actually holding up a lock whereas readLock() doesn't; so why not simply using synchronized instead of readLock() if only implementing a thread-safe block?

2. Extend the above question, can a write thread place a lock on a share object while a reader thread has already held up a lock on the object and is processing some operations within the readLock block?

3. Can I say that the main purpose of readLock, other than polled, timed, and interruptible lock acquisition, is to guarantee memory visibility?

4. Is there any pre-defined preferences for readLock() or writeLock() acquisition?

5. According to Peter Haggar's article, Double-checked locking and the singleton pattern, http://www.ibm.com/developerworks/java/library/j-dcl.html,

even a modifed version of double-checked locking trying to prevent the out-of-order write problem:-

publicstatic Singleton getInstance()

{

if (instance ==null)

{

synchronized(Singleton.class){

Singleton inst = instance;

if (inst ==null)

{

synchronized(Singleton.class){

inst =new Singleton();

}

instance = inst;

}

}

}

return instance;

}

but double-checked locking is still no guarantee on single processor or multi-processors. Is this solved on the newly depolyed JVM?

I would be appreciated if I can have your input; if possible, could you suggest any material for my further reading?

Thanks in advance

[2347 byte] By [bb1234a] at [2007-11-27 10:47:19]
# 1

> 1. As I know the writeLock() is actually holding up a

> lock whereas readLock() doesn't

Untrue. That's not what it says in the Javadoc. It's also not what you say below. Where did you get this idea?

> so why not simply

> using synchronized instead of readLock() if only

> implementing a thread-safe block?

Because read locks can be shared.

> 2. Extend the above question, can a write thread

> place a lock on a share object while a reader thread

> has already held up a lock on the object and is

> processing some operations within the readLock

> block?

No. NB you're now saying a readLock does hold up a lock.

> 3. Can I say that the main purpose of readLock, other

> than polled, timed, and interruptible lock

> acquisition, is to guarantee memory visibility?

No.

> 4. Is there any pre-defined preferences for

> readLock() or writeLock() acquisition?

Yes. A read lock can be shared. A write lock can't.

ejpa at 2007-7-28 20:24:14 > top of Java-index,Core,Core APIs...