Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

isAlive returns false, but thread start throws java.lang.IllegalThreadStateException

 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a singleton setup to run a thread. In the singleton, I check if the thread exists and create it if it doesn't it. I then call isAlive, and if it returns false, I call the thread start method. In the start method, I override the thread method to check if the thread isAlive, and if it's not, start it. I didn't realize I called isAlive twice until I ran into this problem.

If I try and start the thread before the old one has finished, it throws an IllegalThreadStateException. What I don't understand is why it's doing that with the isAlive check before calling start? How else can I check if the thread exists before I call the start method?

Here is my singleton start method.


Here's the one from the thread.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, this is weird. I just made five rapid calls to start the thread and everything worked as it was supposed to. But if I do it from my servlets in Tomcat, I get the IllegalThreadStateException.
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A thread can't be restarted -- even if the previous run terminates and is no longer alive.

Henry
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:A thread can't be restarted -- even if the previous run terminates and is no longer alive.

Henry


Really? I could have sworn I'd done it before.

That would definitely explain the problems I'm having.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bai Shen wrote:
Henry Wong wrote:A thread can't be restarted -- even if the previous run terminates and is no longer alive.

Henry


Really? I could have sworn I'd done it before.

That would definitely explain the problems I'm having.


Really. From the Thread API start() 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
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how I managed to go this long without realising that you couldn't start thread more than once. Here's what I changed my code to. It seems to work, but I haven't finished testing to make sure. Is this a good pattern or is there a batter way to do this?

 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how you can ever call "new Thread(this)" more than once (you never set "et" to null). So why not skip the lazy initialization and create the thread in the constructor?

But then once your thread is finished, you can't start it again and you can't create a new one, because you're enforcing a singleton condition on the class. What's the point of that?
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't see how you can ever call "new Thread(this)" more than once (you never set "et" to null). So why not skip the lazy initialization and create the thread in the constructor?

But then once your thread is finished, you can't start it again and you can't create a new one, because you're enforcing a singleton condition on the class. What's the point of that?


Well, what I did was instead of creating a thread and restarting it, I just created a thread and put it to sleep once it's finished what it was doing. Then when I call start again, I wake the thread back up. So I'm still doing one thread, but I keep it running the whole time instead of starting/stopping it.

As for the lazy initilization, I could do that. That's just a leftover from my previous setup.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic