Stopping and starting thread for multiple key clicks
Ben Watt
Greenhorn
Joined: Feb 13, 2009
Posts: 8
posted
0
I have a thread that calls repaint() which draws a character jumping using certain images.
I press the space bar to make the character jump and whatnot.
I'm using a loop and a try-catch statement to keep track of which image I want drawn.
My problem is, I need the thread to stop going, so that when I move the character, I can jump again, but it says "IllegalThreadStateException" and highlights the "clockThread.start()" line of when I press the Space key.
It makes sense, because I never put a statement to stop it anywhere, and it can't start again.
I need to know where I can stop the thread, at what point?
And also - everywhere I put it, I get a "Supress warning for 'deprecation'"
Any help is appreciated. I can post code as well but it'll take about a day.
First of all, stopping a thread is deprecated. There are some race conditions related to stopping a thread that can leave internal JVM variables in an unwanted state -- don't use it. Second, even if stop() was allowed, you can't restart the thread via the start() method. It is just not allowed.
I am assuming by stop and start, you want the other thread to wait for the first thread. to get some stuff done before it goes and does some work. The way to do this is cooperatively.... You actually have to code the other thread to wait for your first thread, to sychronize between the two threads, and to use notifications to trigger each other, etc.
And BTW, Swing (and AWT) has some threading requirements -- so, you may have to explain a bit more, as not all operations are allowed in any thread.
Henry
Alan Mehio
Ranch Hand
Joined: Apr 04, 2005
Posts: 70
posted
0
Ben Watt wrote:I have a thread that calls repaint() which draws a character jumping using certain images..
You are not allowed to create thread which calls the Swing component to do rendering but rather you put such a request for repaint on the
EventDispatchThread. Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. Most of the Swing method are not thread safe.
ex:
Ben Watt wrote:
I press the space bar to make the character jump and whatnot.
I'm using a loop and a try-catch statement to keep track of which image I want drawn.
My problem is, I need the thread to stop going, so that when I move the character, I can jump again, but it says "IllegalThreadStateException" and highlights the "clockThread.start()" line of when I press the Space key.
It makes sense, because I never put a statement to stop it anywhere, and it can't start again.
I need to know where I can stop the thread, at what point?
And also - everywhere I put it, I get a "Supress warning for 'deprecation'"
Any help is appreciated. I can post code as well but it'll take about a day.
You need to do something like; Foo will have a reference to the swing object which is not seen below
Ben Watt wrote:I have a thread that calls repaint() which draws a character jumping using certain images..
You are not allowed to create thread which calls the Swing component to do rendering but rather you put such a request for repaint on the
EventDispatchThread. Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. Most of the Swing method are not thread safe.
ex: ....
Actually, the repaint() method is one of the few exceptions to this rule. You can actually request a repaint, by calling the repaint() method, from any thread.
Henry
Alan Mehio
Ranch Hand
Joined: Apr 04, 2005
Posts: 70
posted
0
Henry Wong wrote:
Alan Mehio wrote:
Ben Watt wrote:I have a thread that calls repaint() which draws a character jumping using certain images..
You are not allowed to create thread which calls the Swing component to do rendering but rather you put such a request for repaint on the
EventDispatchThread. Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invokes Swing methods also runs on this thread. Most of the Swing method are not thread safe.
ex: ....
Actually, the repaint() method is one of the few exceptions to this rule. You can actually request a repaint, by calling the repaint() method, from any thread.
Henry
Henry,
Thanks for your comment. Yes I see your point plus the revalidate(), and invalidate() methods which I missed to mention.
Ben Watt
Greenhorn
Joined: Feb 13, 2009
Posts: 8
posted
0
All of this has been very helpful.
I am a first year java programmer and have a hard time understanding much of what y'all are saying.
Here is the code I used for my program, hopefully this is more informative than my shoddy explanation to start with.
To repeat, All I want to do is reset the Thread and have it start again each time I click the Space key.
And if there are any other general programming etiquette problems, I'd appreciate the help.