tags:

views:

109

answers:

4

Hello all, Suppose i have one simple function in my program. Whenever i call that function does a new thread or process is spawned to execute the function or it is executed under the main thread memory space only. Please help... any pointers will be appreciated.

Thanks in advance, Rupesh

+4  A: 

When you call a method in Java it will run within the same thread of execution as the code that called it. Unless you explicitly create a new thread within the body of the method.

DaveJohnston
Any explanation for the downvote?
DaveJohnston
A: 

Called function is executed in the same thread.

Advice: you should start learning programming from C. Then the underhood functioning of Java (or anything else) won't confuse you.

Vladimir Dyuzhev
-1 - bad advice, IMO.
Stephen C
bad comment, IMO.
Vladimir Dyuzhev
A: 

When you call a method, processing just moves into that method. This is a general rule across pretty much every language.

Curtis
A: 

As Dave Johnston said, no - unless you explicitly create a new Thread.

Of course, you can get new threads turning up if the method you call creates new threads as part of how it works.

There's a difference between threads and processes. Threads are Java's solution to multi-tasking (and a good solution it is too). A process is an operating system thing. Depending on your JVM, a new thread may or may not run in a new process.

Either way, all threads within a JVM access the same memory space. Slightly paranoid note: there are some synchronisation issues within JVM memory to do with CPU-level memory caches - see the documentation about the volatile keyword if you're doing serious multithreaded coding.

You can have multiple JVMs running with separate memory, e.g. if you run separate commands from the command line.

Daniel Winterstein