GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes isAlive returns false, but thread start throws java.lang.IllegalThreadStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "isAlive returns false, but thread start throws java.lang.IllegalThreadStateException" Watch "isAlive returns false, but thread start throws java.lang.IllegalThreadStateException" New topic
Author

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

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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

Joined: Sep 24, 2008
Posts: 323
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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

A thread can't be restarted -- even if the previous run terminates and is no longer alive.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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


Steve
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Sep 24, 2008
Posts: 323
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.
 
GeeCON Prague 2014
 
subject: isAlive returns false, but thread start throws java.lang.IllegalThreadStateException