I have some code here and it seems to run into an IllegalThreadStateException
and I don't know why. By doing this, it runs two codes at the same time. What I am trying to do is that if I enter anything (word, number, character) the countdown will restart. But whenever I enter something before the countdown stops, it always runs to that exception. What am I doing wrong and is there something I can do to correct it?
Asked
Active
Viewed 405 times
-3

user6345865
- 53
- 7
-
3Don't use a picture for displaying text. – Kayaman May 17 '16 at 13:44
-
Please post your code and the stack trace here. – hinneLinks May 17 '16 at 13:47
-
You are starting the same thread multiple times - you cannot do that (javadoc specify that it will cause IllegalThreadStateException). Next thing - do not use stop method - it is considered as wrongly written – Michał Przybylak May 17 '16 at 13:47
-
Sorry, it can't copy paste my code to here.// – user6345865 May 17 '16 at 13:49
1 Answers
0
You can't call start on a thread more than once. When you call start the second time you'll get an IllegalStateException.
Move the initialization of exp to within the for loop so that start is called on a different instance each time.

Nathan Hughes
- 94,330
- 19
- 181
- 276
-
-
@user6345865: great. also check out [my answer here](http://stackoverflow.com/a/5915306/217324) which has an example of how to terminate a thread using interrupt instead of stop. – Nathan Hughes May 17 '16 at 13:57
-
-
@user6345865: stop is deprecated because how it's unsafe, it releases all locks and may prevent the thread from cleaning up properly. interruption notifies the thread that it needs to end but leaves when it terminates up to the thread. – Nathan Hughes May 17 '16 at 14:42
-