Weird problem using Threads
From: Poochie (poochie2NO_SPAM_at_libero.it)
Date: 06/27/04
- Previous message: Michael: "Re: Why Java DataOuputStream is slow?"
- Next in thread: Jack Barlow: "Re: Weird problem using Threads"
- Reply: Jack Barlow: "Re: Weird problem using Threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 27 Jun 2004 15:59:23 GMT
It's a bit hard to try to explain what appens with my program, but I'll
try:
We have a checkers game, and the problem is when I have the CPU playing
against itself
>From a Menu Item I set the game mode to CPU vs CPU.
Then I start a new game wich sets up the board and then it calls
ProcessUpcomingMove() which is called after any change (new game or move)
is done to the current game.
What the program does, in CPU vs CPU, is call ProcessUpcomingMove(), this
will have the program itself select a move from a JList that contains all
the legal moves and then call the event of the "make move button", wich
applies the changes and recursively calls the ProcessUpcomingMove(). Of
course in CPU vs CPU it'll end only after the game is over.
Currently the code which selects the move for the computer is in
ProcessUpcomingMove() and it just works very fine, never had an error of
any kind. The problem, of course, is that until the game between the
machine and itself is over, the GUI of the program is stuck as its thread
is busy doing the moves.
So, I thought, I should have another thread doing the ProcessUpcomingMove()
work, and then the GUI will still work and I'll be able to change game mode
(player vs player for example) while the computer is playing against
itself. So I amde a new class, which gets the class of
ProcessUpcomingMove() as a parameter and does exactly what
ProcessUpcomingMove() did before, I cut the code from there and pasted in
the new class and I should say it would work. The WEIRD problem is that,
seldom and at random, I get a
java.lang.ArrayIndexOutOfBoundsException: 6 >= 1
or similar in a DefaultListModel variable, which is related to the JList
containing the moves.
What I know for sure is that the code which selects a move from there never
gets out of bounds (as without threads it does work)... the code of
ProcessUpcomingMove() with no thread was like:
see whose turn it is and if the set mode needs so, select a move...;
click on the make_move_button;
After the thread issue it's:
Thread t1 = new Thread(new ProcessMoveThread(this));
t1.start();
Where ProcessMoveThread does:
see whose turn it is and if the set mode needs so, select a move...;
click on the make_move_button;
Indipendently from getting or not the random exception, the game itself
will seem normal scrolling though the moves history (no weird moves or one
side moving twice in a row...) but I don't get the point of these
exceptions it seems like something related to the recursive self call of
ProcessUpcomingMove() but I got no clue.
Last try:
if I modify the threaded ProcessUpcomingMove() code to this:
Thread t1 = new Thread(new ProcessMoveThread(this));
t1.start();
try {t1.join();}
catch(InterruptedException e) {}
click on the make_move_button; //(of couse putting this here means I
removed it from the ProcessMoveThread)
Well, now it dows work with absolutely no exception, but the thread is
usless ans the GUI will obviously get stuck again due to the join()
command.
Any hint?
Hoping that this get understandable,
Poochieİ
- Previous message: Michael: "Re: Why Java DataOuputStream is slow?"
- Next in thread: Jack Barlow: "Re: Weird problem using Threads"
- Reply: Jack Barlow: "Re: Weird problem using Threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]