• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Synchronizing a block on an object.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we synchronize a block of code (not method) on an object, does that mean only one thread can execute that block of code and manipulate the instance variables in that object? I want to know how synchronizing a block of code works.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vijayakrishna Mikkilineni wrote:If we synchronize a block of code (not method) on an object, does that mean only one thread can execute that block of code and manipulate the instance variables in that object? I want to know how synchronizing a block of code works.



Only one thread can own the lock on an object at a time. So, if more than one thread wants to execute the same block of code, using the same synchronization object, only one can run at a time. Furthermore, if different blocks of code want to use the same synchronization object, then only once can run at a time, as well -- even for different blocks of code.

Henry
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, it's not necessary to synchronize a method that does NOT manipulate instance variables, right?

Thank you!
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Leandro Coutinho wrote:Henry, it's not necessary to synchronize a method that does NOT manipulate instance variables, right?


Yes its not necessary, but its done for thread safety...
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:Yes its not necessary, but its done for thread safety...


Why is it done for thread safety if local variables are not shared between threads? I didn't understand what you mean.
 
Ankit Garg
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I read what you wrote wrong, yes its not necessary to synchronize a method which don't manipulate/access instance variables...
 
this is supposed to be a surprise, but it smells like a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic