• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Access to static fields and non static fields

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



However—what if you have a non-static method that accesses a static field?
Or a static method that accesses a non-static field (using an instance)? In
these cases things start to get messy quickly, and there's a very good chance that
things will not work the way you want. If you've got a static method accessing a
non-static field, and you synchronize the method, you acquire a lock on the Class
object. But what if there's another method that also accesses the non-static field,
this time using a non-static method? It probably synchronizes on the current
instance (this) instead. Remember that a static synchronized method and a
non-static synchronized method will not block each other—they can run at
the same time. Similarly, if you access a static field using a non-static method,
two threads might invoke that method using two different this instances. Which
means they won't block each other, because they use different locks. Which means
two threads are simultaneously accessing the same static field—exactly the sort of
thing we're trying to prevent.
It gets very confusing trying to imagine all the weird things that can happen here.
To keep things simple: in order to make a class thread-safe, methods that access
changeable fields need to be synchronized.
Access to static fields should be done from static synchronized methods. Access
to non-static fields should be done from non-static synchronized methods.




Why we have to fallow this ifade
"Access to static fields should be done from static synchronized methods. Access to non-static fields should be done from non-static synchronized methods."
 
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have not read the whole think you quoted to be honest, however having read your question. In a multithreaded environment it is indispensable synchronizing the setters methods if you want a consistent result when you then retrieve the value.

Check this out: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html

It is fundamental though only within a multi-threaded environment.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic