This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Basic multiple locks in one class question  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Am I correct in assuming that a lock behaves similarly to synchronized code in the following way - If I have the following class below:

Class X {
private ReentrantLock lock = new ReentrantLock();

public increment() {
lock.lock();
try {

// some stuff
} finally {
lock.unlock();
}

public decrement() {
lock.lock();
try {

// and some other stuff
} finally {
lock.unlock();
}
}

public otherMethod() {
try {
// more other stuff but no lock
}
}

}

Then if thread #1 start to execute the increment method, then thread #2 cannot begin executing the decrement method but can execute the otherMethod because the lock code executed in the increment method also stops thread #2 from executing decrement?

Thanks
Dave
 
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello David, you're right the ReentrantLock it's the same as the synchronized keyword. With the reentrantlock you can do somethings that with the synchronized you cannot like the posibility to take the lock in a method and release the lock in other method with the synchonized it's automatically taken and released when the scope start/ends.

Regards,

Angel
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!