aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes About lock() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "About lock() method" Watch "About lock() method" New topic
Author

About lock() method

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Regarding to Monkhouse P.141,
line 134- 136, there is a code like this:


If mulitple threads have buildRecordNumbers = true , when one thread acquires the write lock, will other threads wait until the first thread releases the write lock?
So, this piece of code is equivalent to wait/notifyAll() ?

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5207
    
  12

Have you already had a look at the javadoc of the used API?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I read the ReentrantReadWriteLock.java developed by Oracle. I paid attention to the Sync static inner class and its acquire method. I read some JavaDoc in the code and it says when there is not read or write lock aquire, the thread can acquire the write lock. That tells me that the write lock can make the code mutual exclusive. When the write lock is acquired by a current thread, other threads cannot write on the file/DB/variable and even read the information.

When I look at the RenentranceReadWriteLock.java, I don't see any keywords like synchronize(), wait() , notifyAll() or notify().
I don't know why these keywords are not used at all in the algorithm.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Helen Ma wrote:When I look at the RenentranceReadWriteLock.java, I don't see any keywords like synchronize(), wait() , notifyAll() or notify().
I don't know why these keywords are not used at all in the algorithm.

Because it is not necessary.

Going to basics - why do we need synchronized block? So that only one thread can enter in that block. Now, how is that different than 'lock' and 'unlock' method? You can think 'lock' method as start of sync block and 'unlock' as end of it.
So,

is same as


Also, instead of wait, notify and notifyAll, we have await, signal and signalAll respectively.
So,

will be


Try writing small producer-consumer code (like above) using 'concurrent' package and you'll find it quite easier than wait/notify stuff.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Thanks, Anayonkar.

In ReentranceReadWriteLock.java, it puts the threads in a queue. If the thread is not the first thread in the queue, it waits.
I guess when the thread unlocks the lock, the next thread in the queue will lock the lock if it is in fair mode.
Do I still need signal or signalAll methods when the lock is unlocked? If the lock is unlocked, do other threads need to be signaled?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
By the way, I think the lock in the above code:


must be a singleton. Is it true?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5207
    
  12

Helen Ma wrote:Is it true?

Yes. It has to be called on the same object. That's why both methods are called on same object (lock) and not seperate objects (e.g. lock1 and lock2)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About lock() method