Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why threads can't be restarted?  RSS feed

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the following in the SCJP 5 book

"Once a thread has been started, it can never be started again"

What is the logic behind the above?




When i run the above it prints "Rajiv" once and
then throws IllegalThreadStateException.

 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A java thread is a like a human life.

After starting it (being born), you cannot start it again (get born again).

If you are trying to make more than 1 thread in the code you posted, you must do something like below:

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Angeles wrote:A java thread is a like a human life.
After starting it (being born), you cannot start it again (get born again).


Not convinced by your analogy.

On a lighter note, I couldn't help but noticing your name and thinking how your statement is in conflict with the belief that Jesus died, but will be born again.
Just an observation. I do not intend to hurt anyone's religious sentiments.

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:I read the following in the SCJP 5 book

"Once a thread has been started, it can never be started again"

What is the logic behind the above?




When i run the above it prints "Rajiv" once and
then throws IllegalThreadStateException.



Not sure what you are asking here... Your book says that a thread can't be restarted. The JavaDoc says that a thread can't be restarted. And then, when you try to start a thread that has already been started, you are surprised to get an exception? Isn't it doing exactly what is defined?

Henry
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry

The query I meant to ask is why it is that we can't restart a thread ,
may be after it has finished with its job.
That way we won't have to create new Thread objects.
What was the logic behind the prevention of restarting a
thread?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:@Henry

The query I meant to ask is why it is that we can't restart a thread ,
may be after it has finished with its job.
That way we won't have to create new Thread objects.
What was the logic behind the prevention of restarting a
thread?


Well, the short answer is ... it is defined that way. Anything more than that would be speculating.


Now... since I like to speculate....

1. I don't think any of the native threading systems allow a restart -- ie. with Solaris and Posix threads, the thread is started as soon as it is created, and there is no start function at all. And hence, no restart function. So, maybe the designers wanted something similar?

2. The Thread object is in use for other purposes -- such as for notification of other threads waiting to join this thread. Perhaps the designer felt it was too complicated, as they have to define what will happen to a thread joining a thread that may be restarted after the join?

3. There are other ways to achieve thread reuse -- such as using a thread pool... Although, this one is unlikely, as the executor classes didn't get added to Java til much later. But maybe the designers were thinking ahead?

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!