Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java methods are reentrant.

 
Jonah Kommu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All Java methods are reentrant. It means several threads can be executing the same code at once.

How is this achieved? assuming all this happens at the byte code level.

Thanks in advance.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say it's achieved by virtue of the fact that nothing prevents it from happening. In general, threads can execute any code you tell them to.

Did you mean to ask how synchronized blocks or locks can be reentrant? That's a different meaning of reentrant than the one you gave.
 
Jonah Kommu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:I'd say it's achieved by virtue of the fact that nothing prevents it from happening. In general, threads can execute any code you tell them to.

Did you mean to ask how synchronized blocks or locks can be reentrant? That's a different meaning of reentrant than the one you gave.


Actually I want to know what happens under the hood to a Java method at runtime.
what exactly resides on the heap for a method..?

And if this method is concurrently run by multiple threads how does the JVM/ processor handle it. (considering the method is not synchronized.)

TIA !
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Kommu wrote:Actually I want to know what happens under the hood to a Java method at runtime.
what exactly resides on the heap for a method..?

Ummm... whatever the method puts there? Plus whatever was on the heap before the method was called.

As a minimum: nothing is created on the heap automatically when a method is called. If, within the method, you do something that creates something on the heap (like say "Object obj = new Object();", then that happens. If you don't, it doesn't.

Is it possible you were really wondering about what happens on the stack, and said heap by mistake?

J Kommu wrote:And if this method is concurrently run by multiple threads how does the JVM/ processor handle it. (considering the method is not synchronized.)

Each thread has its own call stack. They share the heap, if they do anything that accesses the heap. But as far as the basic mechanism of calling a method from a thread, each thread stores the information they need to do that (mostly the method parameters and the return address, i.e. what-was-I-doing-when-I-called-this-method, get pushed onto the stack for that thread, when the method is called. And popped off of the stack when the method exits.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic