GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Stopping and starting thread for multiple key clicks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Stopping and starting thread for multiple key clicks" Watch "Stopping and starting thread for multiple key clicks" New topic
Author

Stopping and starting thread for multiple key clicks

Ben Watt
Greenhorn

Joined: Feb 13, 2009
Posts: 8
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.


All we are, are just zeros and ones
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40


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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40



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: 73
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


Hope this could help




Regards,
Alan Mehio
London, UK
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

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
Alan Mehio
Ranch Hand

Joined: Apr 04, 2005
Posts: 73
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
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

Instead of the space key being used to start / stop the thread -- why don't you start the thread right away. And key that thread running.

You can modify the thread to check a flag to decide whether to call the repaint method. And then you can change your key listener to toggle that flag.

Henry
 
GeeCON Prague 2014
 
subject: Stopping and starting thread for multiple key clicks