Re: one thread is created for each object construction?
Steve wrote:
For each object construction (for example, Emp e = new Emp(); ), does
the JVM creates one thread for each object construction?
Twisted wrote:
Only if the object's class extends Thread. Even then the construction
runs in the thread where the "new" expression occurs. The new thread
only actually begins to run when its "start" method (inherited from
Thread) is invoked.
Ishwor Gurung wrote:
doesn't thread get created ? in the JVM ? AFAIK, JVM is one big process
We have a contexzt mismatch here.
Twisted was referring to threads spawned by application code. Ishwor seems to
be talking about threads managed internally by the JVM, such as the garbage
collector (GC) thread.
Threads do get created in the JVM, but the ones of interest are the
application threads, of which there is only one until the programmer decides
otherwise.
(running as java/java.exe) but the fragments that we initialise, i.e., in
OPs case, the object "e". Isn't it a thread in JVM? it def can't be a
process. ??? any ideas?
No, construction does not spawn a new thread.
Let me sort of rephrase my question, javac/javac.exe creates one big chunk
of bytecode, JVM reads line by line of that bytecode and instantiates as
per the lines in the code. Now, if if it was a process, we would surely see
it when we do "ps -ef". But we dont!. we see it as "java <something>"
instead. so how does that explain things ?
The application is not a process to the OS (usually), but java is. That
doesn't mean it's automatically multithreaded, other than the GC thread.
Whatever threads a Java application has, including the one main thread, are
part of the "java" process. That is correct.
It doesn't explain anything regarding the OP's question.
The OP had asked if constructing an object spawned a new thread. It does not.
--
Lew