Solved

thread state

Posted on 2014-02-07
7
318 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
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 7

Author Comment

by:gudii9
Comment Utility
RunnableJob is running

where this line is printing. please advise
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
RunnableJob.run() prints it.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
who is triggering
RunnableJob.run()

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

Expert Comment

by:CPColin
Comment Utility
Yes.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now