Java Thread Problem

Hello dear monkeys developers,

How can i finish a thread in java so it can be started again?

[java]protected Thread spell01 = new Thread() {

public void run() {

try {

Thread.sleep(1000);

} catch (InterruptedException ex) {

Logger.getLogger(ObjectParent.class.getName()).log(Level.SEVERE, null, ex);

}

isCoolDown[0] = false;

try {

spell01.join();

spell01.interrupt();

} catch (InterruptedException ex) {

Logger.getLogger(ObjectParent.class.getName()).log(Level.SEVERE, null, ex);

}

}

};[/java]

When i start this thread once, it works fine, but when i start it a second one ( and i know it has finished what i needed ) it gives me this error :

[java]SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.IllegalThreadStateException

at java.lang.Thread.start(Thread.java:682)

at Objects.Players.Player2D.getKeyBoardInput(Player2D.java:65)[/java]

Any suggestion dear monkeys?

Thanks anyway!

ERm… what are planning to archive? Also why do you join the thread with itself?



If you want a timer system there are better ways. eg save system.currentTimeinMillies + 1000 in a field, then in update check if system.curren… is larger than the stored value.

Well i read somewhere that somethread.join(); makes the thread stop

You need to find a solid threading tutorial and go through it in detail. Once you understand that read the threading documentation in the wiki to see how you apply that knowledge in JME3…

If you start a thread for logic processing, you’ll probably not want the thread to die. You can keep it running in a while(boolean) {doSomething} loop. The thread will die when the boolean turns false. Then you feed the thread with your game logic inputs and check for outputs. You can look up the Producer/Consumer paradigm for getting data in and out of your thread. The thread can run on lowest priority if it is processing only sundry data.

Best not to roll your own producer/consumer thing really when java.util.concurrent provides everything you need in the executors.



I agree with zarch, do a good tutorial (many actually) because threading is tricky and you are only just getting started. You have many mistakes left ahead of you and some solid tutorial background will help tremendously.



Also, try to look for java.util.concurrent related tutorials. Anything else is most likely old and crusty.

No i am not just getting started, it just i didnt know why it gave me that error, but i got rid of the Thread and made it work

Thanks anyway all :slight_smile:

1 Like
@mathieu-roux222 said:
No i am not just getting started, it just i didnt know why it gave me that error, but i got rid of the Thread and made it work


Because threads can only be started once. This is pretty well known, I think... and starting a thread each time is expensive enough that you don't want to do it a lot, anyway. That's why the included thread pool stuff goes through trouble to keep threads around rather than starting them all the time.

You also can't join yourself. Joining is for another thread to use to wait for a different one to finish. And join() then interrupt() makes no sense at all.

These are all pretty basic threading related things so you'll have to forgive me if I thought you were just getting started with threads.

No it’s ok, but i read somewhere that someThread.join() makes the thread end itself, well now i know it doesnt do that xD

@mathieu-roux222 said:
No it's ok, but i read somewhere that someThread.join() makes the thread end itself, well now i know it doesnt do that xD


The javadoc is the official source of documentation.
http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#join()

A java developer that doesn't have the JDK javadocs a few keystrokes away isn't really a java developer. ;)

I have the SDK to tell me what it does, but i didn’t look because i though i knew what it does.

Anyway i got it working by making another way than using Threads.

Anyway thanks to all monkeys who helped me :slight_smile: