File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Q from Sierra & Bates SCJP Study Guide

 
Bruce Evans
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
MannY Gates
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I try this code (I renamed your class, your name is very poorly chosen) and get an exception as expected...

 
MannY Gates
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same thing:
 
MannY Gates
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might be a bug in your compiler or runtime version. I was using 1.5.0 (or was that 5.0 ).
 
Bruce Evans
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey bruce please post the details of Bert's answer here whenever they are available.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry and Jeroen
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic