• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!