• Post Reply Bookmark Topic Watch Topic
  • New Topic

Couple of questions on Threading

 
Siva Prasad Reddy Katamreddy
Ranch Hand
Posts: 32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
Can some one please clarify the following:

1. In order to execute a synchronized method JVM needs to lock the monitor associated with the Object. JVM doesn't need to get the Lock on the object in order to run a non-synchronized method?
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?
3. Each method invocation will be executed on a new stack. If a method invoke another method then one more stack will be created?

Thanks,
Siva.
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1> Correct
2> Allowed
3> It is rather each thread will be having it's own stack of execution
 
Siva Prasad Reddy Katamreddy
Ranch Hand
Posts: 32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for your quick reply.

Regarding the 2#. Could you please elaborate how two threads can access the same method body.

Suppose the method is as follows:

public void myMethod()
{
int a = a+1;
int b= a*a;
....
.....
int c = b / a;

}

So here if thread1 is currently executing second statement, will the byte code related to statement2 is loaded into the thread1 stack and execute?
What if at the same time thread2 tries to execute the first statement? Both threads will be executed in parallel by doing context switching?

Or is there any other way?
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each thread maintain it's local copy of variable to work on. So if you don't synchronize and both thread are using same shared data then there are chances that both will be having different local value of same data. And you loose data integrity.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Siva Prasad Reddy Katamreddy
Ranch Hand
Posts: 32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

Thank you so much for your detailed explanation.

Thanks,
Siva
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!