Your question #2 is asked by many.
Siva Prasad Reddy Katamreddy wrote:... 2) When the JVM loads a class all the method body/code is stored in method area which is a
shared area. What if two threads try to invoke the same non-synchronized method on different
objects. Will the two objects allowed to execute the method concurrently? ...
Only one thread runs at a time with the JVM switching from thread to thread for many different
reasons. Also, as you said, there is only one copy of each method, and only one copy of static
class variables.
Here's the trick. Each object instance is given its own set of variables which determine the object's
state. Although the method code is shared, it has no state. So when the JVM stops a thread, it only
needs to keep track of where to start up again.
Synchronization is used to prevent a second thread (using a method, of course) from changing
an object's state until the first thread (holding the object lock) completes the method.
Jim ...
...