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 Thread Q from Sierra & Bates SCJP Study Guide Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Q from Sierra & Bates SCJP Study Guide" Watch "Thread Q from Sierra & Bates SCJP Study Guide" New topic
Author

Thread Q from Sierra & Bates SCJP Study Guide

Bruce Evans
Greenhorn

Joined: Nov 28, 2004
Posts: 14
I have read in S&B Chap 9 page 508 that when a thread is
Dead, then calling start() on that thread will throw a
Runtime Exception (RTE).

I wanted to see this and wrote to programs, but no RTE is
thrown. The only way I get a RTE is if I call start() again
while the thread is alive.

Has anyone ran across this before where they get a RTE by calling
start() on a dead thread?

TIA

-Bruce



Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is what the Thread.start() documentation (Java 5) says:

start

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Throws:
IllegalThreadStateException - if the thread was already started.
See Also:
run(), stop()



It does say "already started" which includes finished (to me). For SCJP I would go with that (and appeal if I failed by one question )


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Dear Barry,

I agree that already includes finished... but Bruce's question still remains unanswered... Why the RTE was not thrown when we called the start method on the dead thread?

The important thing here to note is that by calling start on dead thread does not restart the thread.


Regards,<br /> <br />MannY<br />----------------------------------------------------------- <br />"Beam me up Scotty, there's no intelligent life down here" !!! <br />------------------------------------------------------------
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
if I try this code (I renamed your class, your name is very poorly chosen) and get an exception as expected...



42
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Jeroen plz try the Bruce's second short code.

[code]
public class thread2 extends Thread {
public static void main(String[] a) {
thread2 t = new thread2();
t.start();
System.out.println("main(): Done starting threads");
System.out.println("Thread t alive? " + t.isAlive());
try{Thread.sleep(1000);} catch (InterruptedException ie){}
System.out.println("Thread t alive? " + t.isAlive());
System.out.println("Starting thread t again...");
t.start();
}
public void run() {
for(int i=0; i<10000; i++);
}
}

[code/]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Same thing:
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Jeroen I am using jdk 1.4.2 to compile and run the program and I am not getting the Runtime exception....

Where is Barry???
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Might be a bug in your compiler or runtime version. I was using 1.5.0 (or was that 5.0 ).
Bruce Evans
Greenhorn

Joined: Nov 28, 2004
Posts: 14
I have sent an email to Bert, so hopefully he can
respond when this warrants his time.

We can't mix apples and bananas here. I am preparing
for the SCJP 1.4 cert, not 5.0

Just an FYI, 'rable' is short for runnable... Make sense now??

-Bruce
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Hey bruce please post the details of Bert's answer here whenever they are available.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
On Java 5.0 both programs throw an IllegalThreadStateException. So the Java 1.4 JVM behaviour of not throwing an exception is not consistent with the API.
In Java 5.0 threads have been overhauled quite a bit, so that could explain the fact we now see the exception being thrown.

For SCJP 1.4 go with the API not with the (mis)implementation.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Definite implementation bug in 1.4 and earlier versions.
I can replicate the problem in 1.3.0 on AIX, 1.4.2 on Win32 and 1.4.1 on Linux.
On 1.5.0 Win32 it behaves as the documentation says it should.

As the exam is about the theory, not the implementation, you should keep to the expected behaviour (however illogical that seems).
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
OK, did some more research (never get me too interested ).

1.4.2 implements Thread.start() as follows:


1.5.0 implements it like this:


So it seems like someone at Sun realised there was a problem with the native implementation not throwing the declared exception and they just wrapped the entire thing in another method
MannY Gates
Ranch Hand

Joined: Nov 05, 2004
Posts: 57
Thanks Barry and Jeroen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Q from Sierra & Bates SCJP Study Guide
 
Similar Threads
Dead Thread !!
Threads
Main Thread is Alive ?
Thread question
Question on Thread