aspose file tools*
The moose likes Java in General and the fly likes Java methods are reentrant. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java methods are reentrant." Watch "Java methods are reentrant." New topic
Author

Java methods are reentrant.

Jonah Kommu
Greenhorn

Joined: Mar 09, 2011
Posts: 12
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Mar 09, 2011
Posts: 12
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
Consider Paul's rocket mass heater.
 
subject: Java methods are reentrant.