• 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

Doubt in Threads

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I call non-synchronized method from another class with lock on that original object(using synchronized block) can any other thread can access the method of the original object?
For e.g I have a class called ClassA and all methods in that class are not synchronized. If I call a method MethodA from ClassB object with lock on object of ClassA. Then, can any other thread access methodA?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For e.g I have a class called ClassA and all methods in that class are not synchronized. If I call a method MethodA from ClassB object with lock on object of ClassA. Then, can any other thread access methodA?



Depends on how MethodA is called. If another thread simply calls MethodA, then yes, another thread can access methodA, concurrently. If there is a requirement that methodA can only be called when you own the lock of ClassA -- with an external synchronized block -- then only one cooperating thread will be able to access methodA at a time.

Henry
 
Someswara Chittlella
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to specify that MethodA can be called only when it owns lock on that object with external synchronization block?


If there is a requirement that methodA can only be called when you own the lock of ClassA

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Someswara Chittlella:
How to specify that MethodA can be called only when it owns lock on that object with external synchronization block?



To enforce it? You can't... It must be cooperative. You can grab the lock of classA before calling a method of classA like this:



Unfortunately, you can't enforce that it *must* be grabbed prior to calling methodA(). If someone else writes code that simply call MethodA(), it will just work in parallel... To do that you have a couple of options.

- You can declare the method as synchronized (or use a synchronize block in the method) -- so it will definitely grab the lock.

- With Java 5, you can actually check to see if the lock is held with the Thread.holdsLock() method.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay... this discussion is more about threads than programmer certification, so I am going to move this to the threads forum.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic