• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About lock() method

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() ?

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic