Solved

thread state

Posted on 2014-02-07
7
322 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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 17 59
Java Restore security prompts not working 10 116
hashmap order 17 40
Java 8 to Java 6 8 32
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

740 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