File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Thread" Watch "Question on Thread" New topic
Author

Question on Thread

Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Hi,
I was going thro chapter 9 on Threads from Kathy & Bert and according to them,"Calling a start() method on a dead thread will give a fat Run time exception".
The following code works fine for me with out throwing any runtime exceptions.

package threads;
import java.*;
/**
* Title: Java Wanna bee
* Description:
* Copyright: Copyright (c) 2004
* Company: Like Minds Consulting,Inc
* @author Ramnath Krishnamurthi
* @version 1.0
*/
public class WatchDog implements Runnable {
public WatchDog() {
}
public void run(){
System.out.println("Barking");
for(int i = 0;i < 10;i++){
System.out.println("bow!bow!");
try{
Thread.sleep(10000);
}catch(InterruptedException ex){
}
}
}
public static void main(String[] args){
Runnable dog = new WatchDog();
Thread t = new Thread(dog);
System.out.println("is Dog Alive?"+t.isAlive());
t.start();
System.out.println("is Dog Alive?"+t.isAlive());
System.out.println("(1) "+t.isAlive());
for(int i = 0; i < 10;i++){
System.out.println(Thread.currentThread().getName());
try{
Thread.sleep(1000);
}catch(InterruptedException ex){
System.out.println("Thead is interrupted");
}
try{
t.join();
}catch(InterruptedException ex){
System.out.println("Thread is interrupted");
}
System.out.println("(2) "+t.isAlive());
System.out.println(Thread.currentThread().isAlive());
}
t.start(); //Shouldn't this line throw a Run time Exception??
}
}
Please help me out.
Thanks in Advance,
Ramnath
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
hmmmmm, who said that :-(
anyway, a dead thread cannot be restarted but u can still have access to its variables and methods. Which means u can still make calls to it! U cant however restart it.
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
after giving it a thought, I think its a matter of which JVM u are working on. RuntimeExceptions are thrown when a method detect it's encountered an error in the virtual machine runtime.
So, maybe it all depends on the JVM u are using ;-)
VP Jain
Ranch Hand

Joined: Feb 11, 2004
Posts: 81
Hi,
Try to modify your code as follows

public class WatchDog implements Runnable{
public void run(){
System.out.println("Barking");
for(int i=0; i < 10; i++){
System.out.println("bow!bow!");

try{
Thread.sleep(100);
}catch(InterruptedException e){}
}
}

public static void main(String[] argv){
Runnable dog = new WatchDog();
Thread t = new Thread(dog); //new state.
System.out.println("is Dog Alive?" + t.isAlive());
t.start();
System.out.println("is Dog Aive?" + t.isAlive());
t.start();

}

}

The following code will compile fine but you will get a runtime exception.

Regards
.PAL.


SCJP SCWCD SCBCD <br /> <br />what u do in life echos in eternity!
VP Jain
Ranch Hand

Joined: Feb 11, 2004
Posts: 81
Hi,
Even if you comment the t.join() method in your lines of code, you will
get a runtime exception.

If u read the chapter u can note that.
A call to join() Guaranteed to cause the current thread to stop executing until the thread it joins with(in other words, the thread it calls wait() on) completes. If the thread it's trying to join with is not alive,however,the
current thread won't need to back out.
I hope that this makes you clear
Bye
.PAL.
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
....I still dont get what PAL is saying,
can u answer this question PAL?
Is it possible to call start() on a dead thread and not get a runtime exception?
if your answer is "no" or is the t.start() call not calling a dead thread. Please, I need some enlightenment.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Ramnath-
For the purpose of the exam, calling the start method on a thread that has already been started will generate a runtime-error (To be more specific illegalThreadStateException).
In reality however, this is NOT the case. If the thread was dead, then the second attempt to start the thread will most likely be ignored by the JVM (depends on the compiler).
For purpose the exam, you should know that the exception is always thrown.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
VP Jain
Ranch Hand

Joined: Feb 11, 2004
Posts: 81
Hi,
For your question
Is it possible to call start() on a dead thread and not get a runtime exception?
The answer is : NO

But i dont get what Vicken is saying about reality.

If you try to start() on a dead thread you will get a runtime exception
invariable of what is the JVM.
Regards
.PAL.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Vijayakeerthy-
This is what i mean by REALITY


As you can see the join method makes sure the thread is dead before calling the start method again(line 1). Now, this code will compile and run printing the letter 'A'. However, if you encounter such question in the exam you should answer that the code throws a runtime error.
I believe that in JVM prior to 1.4, the pre-mentioned code will indeed throw an exception, however in the 1.4 version this is not the case.
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Hello All,
Sorry for the delayed reply since i was in a diffn time zone than all of u guys.But am still not convinced about the various answers.But i figured that there is something to do with the join() method call in the thread.I have not got past that point in my learning curve.Anyways thanks for all the answers and infact commenting that join() in the code threw me an IllegalStateException.Once again thanks for all ur valuable inputs.
Ramnath
"Feed an opportunity: Starve a problem"
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
but I compiled and ran the code(without the package thread) on my system and it worked just fine. Why?
I need some explanation, my exam will be sometime later next week!
here is the result I got:
is Dog Alive?false
is Dog Alive?true
(1) true
main
Barking
bow!bow!
bow!bow!
bow!bow!
bow!bow!
bow!bow!
bow!bow!
bow!bow!
bow!bow!
bow!bow!
bow!bow!
(2) false
true
main
(2) false
true
main
(2) false
true
main
(2) false
true
main
(2) false
true
main
(2) false
true
main
(2) false
true
main
(2) false
true
main
(2) false
true
main
(2) false
true

...sorry for the length but I really do need an explanation
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Crusty,
Did u comment out the t.join() call in my code and execute it??.It does throw an exception.
Thanks,
Ramnath
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
>>>Did u comment out the t.join() call in my code and execute it??.It does throw an exception.
Thanks,
Ramnath

I didnt comment it out, the only alteration I made was not including a package
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Looks like Vicken is right in his theory.Calling the t.join() does make the main thread to wait and when i called t.start() again,the JVM ignores it since "t" is already dead.
After removing or commenting out the t.join() ,i immediately got an illegalStateException thrown during the run time.The reason i think is that main thread is trying to attempt to do t.start() on the already running thread.
Crusty ,i hope now you are clear with the reason why i was not able to get the runtime exception.
HTH,
Ramnath
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
thanks guys, and thanks Ramnath, I got the gist of it.
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
Originally posted by Vicken Karaoghlanian:
Ramnath-
For the purpose of the exam, calling the start method on a thread that has already been started will generate a runtime-error (To be more specific illegalThreadStateException).
In reality however, this is NOT the case.

This is what's annoying me the most about the exam: "For the purpose of the exam this is true... but in reality however this is false ...". It's the second time I find this kind of argument. I find it really hard to study something, understand it and then ignore it when I'm doing the exam. What things should I ignore then? It starts playing with my mind: "I know this is false but maybe for the purpose of the exam it's true" (like saying null is a keyword when it's just a reserved word, a literal).
I know this is the kind of thing you can't do anything about, I'm just sharing my frustrations ....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Thread
 
Similar Threads
Thread Q from Sierra & Bates SCJP Study Guide
Dead Thread !!
Thread,Join problem
Main Thread is Alive ?
Thread Problem