• 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

Unlocking while downgrading lock

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a general question regarding the correct usage of a ReadWriteLock in Java 5.

Andrew's book states that if you use a lock, it's best to unlock in a finally clause, like here:


But what is the best way to unlock if you downgrade from a writeLock to a readLock?

Now I have



The readLocked and writeLocked boolean's are required to guarantee unlocking in case of an error but to prevent IllegalMonitorState exceptions to occur if no errors occur.
But I don't really feel comfortable with this solution. Does anyone have a suggestion on how to improve this?

Thanks a lot.

Luc Feys
 
Luc Feys
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solved. With a little bit of refactoring I was able to restructure my code. Now I have the following



Quite simple actually. Don't know why I didn't see this last night.

Luc Feys
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic