Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Threads: subtle detail calling start() important?

 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In K&B Chapter 9 on threads is says:
Dead A thread is considered dead when its run()
method completes. It may still be a viable Thread object, but
it is no longer a separate thread of execution. Once a thread
is dead, it can never be brought back to life! (The whole "I
see dead threads" thing.) If you invoke start() on a dead Thread
instance, you�ll get a runtime (not compiler) exception.


The following code does not give me an java.lang.IllegalThreadStateException


it compiles and runs fine.

The answer to this is (from "Java Threads, 2nd Edition" by S. Oaks
and H. Wong, Chapter 2.6)

What happens when you try to restart a thread? The answer is
that it actually depends on when you restart it
...
it actually takes time for a thread to stop.
...
If the start() method is called after the stopping thread has actually
stopped, nothing happens: the thread object is in a state where it
cannot be restarted


Can an exam question go into such low level details?

Cheers,

Gian Franco
[ May 20, 2004: Message edited by: Gian Franco Casula ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read this thread?

And from the books errata:

Page 505,Second paragraph2/07/03

Change:
.. method has finished, you can't say t.start(); you'll get a big fat runtime exception.
To:
.. method has finished, calling t.start() won't restart it.



The errata is here.
[ May 20, 2004: Message edited by: Barry Gaunt ]
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry, I didn't see that topic-thread before.

Cheers,

Gian Franco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic