• 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

Static Synchronized Methods

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the following method existed:
class MyClass {
synchronized void insync() {}
void notsynchronized() {}
static synchronized void staticsynctest {}
}
What would happen if staticsynctest were called?
Would I have access to insync, or notsynchronized?
Would I have access to static class data or class data?
Would I have access to instance data?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each object has a lock(monitor) ,and each class also has a lock(monitor),the lock on objects is completely independent of the class lock and vice-versa.
So if you call a synchronized static method ,then that thread gets the lock of the class ,but other thread can call instance methods(synchronized ot others)
Hope this helps
M
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about the access to class variable(static) when static synchronized method is called???
also
access to instance variable when synchronized instance metod is called ???
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is important to remeber that Static methods are run on the class, not the instance. It woun't have any reference to the instance, unless you take extra messures to make sure that the method has it (like sending it as a parameter, or seting a static variable to point to it). Thus you wont be able to access anything that is assosiated with th instans, i.e. everything not static in the class. There is no 'this' in static methods.
So to answer your initial questions:
No access to insync() or notsynchronized().
Only static class data.
No instance data.

Though, if you wanted free access to the class, you could change the method to:

and call it with for example staticsynctest(this);
Was that the answer you where looking for?
/Mike
 
Charlie Swanson
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, thanks for the answers. There is another point I am sure not clear on. When a static synchronized method is called, I understand that the static methods will be locked.
What happens to a static class variable? Would it also be locked?
The thing I am confused on is non-static variables put a lock on the object. In static synchronized methods there is no instance object to lock. What exactly is the lock on?

 
reply
    Bookmark Topic Watch Topic
  • New Topic