• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Couple of questions on Threading

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jim,

Thank you so much for your detailed explanation.

Thanks,
Siva
reply
    Bookmark Topic Watch Topic
  • New Topic