Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

thread state

Posted on 2014-02-07
7
Medium Priority
?
327 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 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