thread state

Hi,

I am working on below example
http://www.avajava.com/tutorials/lessons/how-do-i-determine-if-a-thread-is-alive-or-not.html

I read as below
After the run() method finishes, the thread is considered to not be alive anymore.

I thought when we say run() thread starts instead.


Also output i got is as below

State:NEW
Is alive?:false
State:RUNNABLE
Is alive?:true
RunnableJob is running
State:TERMINATED
Is alive?:false



I am not clear on the output as well.
why the State is printed 3 times. Are we having 3 threads here.
Please advise
LVL 7
gudii9Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CPColinConnect With a Mentor Senior Java ArchitectCommented:
why above line printed only once. I thought it should be called and printed 3 times too.

What code would call RunnableJob.run() three times?

As above why Is alive? is false for one thread whereas Is alive is true for other thread

The isAlive() method is being called on the same thread three times. The last paragraph of the tutorial explains what's going on. Please re-read that part.
0
 
CPColinSenior Java ArchitectCommented:
The thread is alive as long as the run() method is still executing. The state prints three times because that's how many times displayStateAndIsAlive() is called from the main() method.
0
 
gudii9Author Commented:
RunnableJob is running

why above line printed only once. I thought it should be called and printed 3 times too.

State:TERMINATED
why State is TERMINATED as above after calling Thread.sleep(1000);


State:NEW
Is alive?:false
State:RUNNABLE
Is alive?:true
As above why Is alive? is false for one thread whereas Is alive is true for other thread
Please advise
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
gudii9Author Commented:
RunnableJob is running

where this line is printing. please advise
0
 
CPColinSenior Java ArchitectCommented:
RunnableJob.run() prints it.
0
 
gudii9Author Commented:
who is triggering
RunnableJob.run()

Is it thread.start(); line in the code. please advise
0
 
CPColinSenior Java ArchitectCommented:
Yes.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.