Thread problem is ruining my life

If you can solve my problem then, in addition to points, you will have my eternal gratitude.
I am designing an applet which features moving graphics.
There is to be a "GameManager" class which will periodically
move all of the graphical objects according to their own
methods(I have this part down & it works). The problem is
that the GameManager is supposed to dynamically alter the
behavior of the graphical object according to user input
(Mouse click events). I have been unable to find a way to interrupt my infinite "move and wait" loop in such a way as to allow mouse clicks to be collected and processed in between moves. I am right now using the VisualAge for Java VM (not that it matters, since all VM's implementations are the same-right;-)?) Anyway, the code I have looks something like this, to give you an idea:

public void run{
while(true){ //Infinite loop
move();
//[NEED METHOD HERE TO YIELD CONTROL UNTIL NEXT MOVEMENT
//SO THAT THE VM CAN PROCESS THE MOUSE CLICKS ANT INFORM
//THIS CLASS OF THEM. EXPEREMENTING WITH A VERY LONG FOR
//LOOP HAS REVEALED THAT THE CLICKS ARE IN FACT NOTED, BUT
//DO NOT GET PROCESSED UNTIL THE LOOP ENDS. I
//HAVE TRIED: t.sleep(), t.sleep(intervalBetweenMoves), //t.yield(), t.setPriority(Thread.PRIORITY_MIN), AND
//VARIOUS PERMUTATIONS. IF ANY OF THESE METHODS ARE INDEED
//THE CORRECT ONES, THEN I AM NOT USING THEM CORRECTLY AND
//NEED TO KNOW THE RIGHT WAY TO USE THEM]
wait(intervalBetweenMoves);
}
Help appreciated!
Ben Drasin

Tried it. Does not work. The main thread never even gets to set up the rest of the applet, because the selfish while loop never gives up control. Sleep does not seem to be effective in this way; as I mentioned, I already tried this. Is there anything else I need to be doing in order to get sleep to work? Or should I be using another method entirely?

You want your run method to execute the code within it for that thread, and it's locking because you are putting it into an endless loop. Your wait(intervalBetweenMoves) is then just t.sleep(intervalBetweenMoves). I know you said this didn't work but I think it should if you just ditch the while(true) loop you're getting stuck in.

I decided to add some code. It's primitive but it demonstrates what I'm getting at. Prints an ever incrementing number to standard output but will also report any events. I've marked it as an answer instead of a comment because if I think this should be what you need.

After re-reading your question, maybe the thing is that you don't want anything other than the sleep in "case 0". Maybe your move() method is too costly to do every 100ms (and maybe it's not necessary until you get an event and set runAction to some non-zero value).

Anyway, the whole point is that you poll some variable in your thread that gets changed in the handleEvent thread. How often you poll is important: too often and you load the CPU, too seldom and you get poor response to the event that set it.

Still no luck. Let me fill you all in on what I have tried so far (I do appreciate your attempts, but regret that I cannot award points :-) ).
In my code for Gamescreen.handleEvent and GameManager.move() I
am System.out.println'ing the current time; this confirms that the events are not registering until after the loop exits (I have been alternating between the while loop and a long for loop). In order to allow the applet to finish loading, I put an extra if statement in handleEvent to start the game on the first sucessful handling of the event. Mabey I am just missing something which you assume I know. I'raise the stakes in view of my increacing desperation. The following is raw, bare, naked code from my applet (well, almost):

In any case, call "start()" on a Thread object to get the "run()" method started. Don't call "run()" directly. The "start()" method returns immediately and starts a new thread. Calling "run()" will block the current thread until it returns.

Anyway, as you can probably tell, ExpertsExchange mangles source code pretty bad. If you want, you can send stuff to me at walters@ct.net

Featured Post

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all. Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects.
Interface Comparable is used to allow objects of a cl…

Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…