Wednesday, October 03, 2012

Yeah, What He Said...

Trying desperately to study up for Oracle's Java exam, but a single sentence carries the same effect as a bottle of dramamine, and a paragraph can put down a horse:
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.
In fairness, I've tried repeatedly to saw through MSDN articles on Microsoft's various "multithreading schemes", but can't saw more than a paragraph or two off before the eyes glaze over. Not from lack of understanding mind you, just utter and complete boredom (though I did chose to work on this rather than subject myself to the presidential debates, so I guess it still trumps some forms of..."entertainment").

No comments: