• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

synchronization and exception  RSS feed

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what i understand about synchronization,the following 2 ways of synchronizing an instance variable are equivalent:

Route 1:

void reverseOrder() {
synchronized (this) {
int halfWay = intArray.length / 2;
for (int i = 0; i < halfWay; ++i) {
.......
......
}
}
}

Route 2:

synchronized void reverseOrder() {

int halfWay = intArray.length / 2;
for (int i = 0; i < halfWay; ++i) {
i....
.......
}
}
}

If so,we can use these 2 techniques inter-changeably to protect the instance variable 'intArray'
Does anyone differ on this?

Now in either of these techniques,if we throw an exception mid-way,does the lock get released automatically?
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, use code tags.

Yes, marking the method as synchronized and synchronizing the whole method body on this object has exactly the same result. And always when you return from a synchronized method or a synchronized block, the lock is released, no matter if it's a normal return or an exception.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!