Solved

thread state

Posted on 2014-02-07
7
325 Views
Last Modified: 2014-02-18
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 39843337
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
 
LVL 7

Author Comment

by:gudii9
ID: 39851181
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
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 39851210
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 7

Author Comment

by:gudii9
ID: 39857569
RunnableJob is running

where this line is printing. please advise
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39857578
RunnableJob.run() prints it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39868846
who is triggering
RunnableJob.run()

Is it thread.start(); line in the code. please advise
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39868854
Yes.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question