I have some questions regarding the usage and significance of the synchronized
keyword.
- What is the significance of the
synchronized
keyword? - When should methods be
synchronized
? - What does it mean programmatically and logically?
javakeywordmultithreadingsynchronized
I have some questions regarding the usage and significance of the synchronized
keyword.
synchronized
keyword?synchronized
?
Best Answer
The
synchronized
keyword is all about different threads reading and writing to the same variables, objects and resources. This is not a trivial topic in Java, but here is a quote from Sun:In a very, very small nutshell: When you have two threads that are reading and writing to the same 'resource', say a variable named
foo
, you need to ensure that these threads access the variable in an atomic way. Without thesynchronized
keyword, your thread 1 may not see the change thread 2 made tofoo
, or worse, it may only be half changed. This would not be what you logically expect.Again, this is a non-trivial topic in Java. To learn more, explore topics here on SO and the Interwebs about:
Keep exploring these topics until the name "Brian Goetz" becomes permanently associated with the term "concurrency" in your brain.