• 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

Synchronization.....

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a Thread1 is executing a synchronized methodA in a class A, Is it possible for another Thread2 to access another non-synchronized methodB in class A?

I dont think it's possbile, because the instance is already locked by Thread1 and with that instance only, you can invoke the mehtodB, which is not possbile, as it is already locked. But is my understanding correct?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Non-synchronized methods do not check the lock status, so they can be called at any time.
 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, object lock is a sort of a flag which will be checked only when synchronized keyword is used in the code?

I thought, the whole object is locked/encapsulated from access for some time.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Locking means prohibiting multiple threads to execute a synchronized method or a synchronized block at same time. This happens by lending the object lock to the currently running thread. But still that object can be used to invoked any method by different threads.

Locking doesn’t mean that the object cannot be used to invoke a method. As Christophe also said "Non-synchronized methods do not check the lock status"

The object can be used to invoke methods by different threads:

1- If the method called is non-syncronized, it will run.
2- If the method called is synchronized and already a thread is running it, then the calling thread will wait to acquire the object lock.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To understand synchronization, focus more on the objects involved and their
lock status rather than the method. The goal is to protect object state as the
JVM shares the CPU among threads; preventing multiple threads from operating
simultaneously on the same object. This does not mean that only one copy of
the method is running. Rather, it means that each copy of the method must
lock and operate on a different object.

Methods are not blocked, nor is access to any object. Non-synchronized code can
trash an object's state even while a synchronized method is holding its lock. This is
why it's so important that all changes to critical objects be done in synchronized code,
so locks are obtained before any changes are made. Missing one can cause trouble.

Jim ... ...
 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you all for the wonderful replies....
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic