• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About lock() method

 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9790
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you already had a look at the javadoc of the used API?
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I think the lock in the above code:


must be a singleton. Is it true?
 
Roel De Nijs
Sheriff
Posts: 9790
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic