call thisThread.run() +! calling thisThread.start() first

2. What will happen when you call thisThread.run() without calling thisThread.start() first?
answer: doesn’t create a thread
i agree. start() is a hook into the JVM.

It sets up the call stack in the JVM stack segment and those internal memory structures (like instruction pointers) needed by a real thread. Then it points the instruction pointer to the run() method’s address. Then it joins the other Eligible threads to be scheduled by the jvm scheduler. I believe this start() runs on the thread-creator thread, but run() would execute on the new thread. If I’m right, then it’s misleading to say start() calls run(). start() returns after setting up the jvm thread. When the new thread is scheduled to execute, first method executed is run().

This jvm thread exists independent of the thisThread object. Jvm thread can run (till shutdown) even after thisThread object is garbage collected. Conversely, thisThread object can live in the heap without the call stack — this is the situation before we call start(), or after the jvm thread exits.

Physically, the jvm thread occupies a bunch of memory cells (its home, in the stack) in order to do its job. Our thisThread object also occupies a bunch of memory cells (in the heap) to hold indirect pointers to the jvm thread’s home.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s