freelanceprogrammers.org Forum Index » Java
Thread Weirdness
Joined: 17 Apr 2004
Posts: 28
Thread Weirdness
Can anyone explain this to me: I think I`ve got the code right, thanks
to the generous example sent to me, but I can`t figure out the cause of
the following test results:
Battle class:
AllyParty ap;
ap.ally[0].advance();
ap.ally[0].retreat();
Ally class:
public synchronized void threadAdvance()
{
System.out.println("Advancing");
hasAdvanced = false;
while (getXPos() > 350)
{
moveLeft(20);
setCurrentImage(WALK);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
setCurrentImage(STAND);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
}
System.out.println("Done advancing");
hasAdvanced = true;
this.notifyAll();
}
public synchronized void threadRetreat()
{
while (hasAdvanced == false)
{
try
{
this.wait();
}
catch (InterruptedException e) {}
}
System.out.println("Retreating");
while (getXPos() <= 500)
{
moveRight(20);
setCurrentImage(WALK);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
setCurrentImage(STAND);
battle.repaint();
try { Thread.sleep(200); } catch (Exception e) {}
}
notifyAll();
}
Test results:
Test Condition: Control group
Test Result: No visible movement of sprite. No console messages
(Was expecting "Advancing" "Retreating")
Test Condition Commented out ap.ally[0].retreat()
Test Result Perfect visual effect. All expected console
messages present.
Test Condition Comment out ap.ally[0].advance(), uncommenting
ap.ally[0].retreat()
Test Result Same result as Control group
Conclusion: Confusion
The case with retreating without advancing makes sense, as does
advancing without retreating, but why doesn`t nothing happen when both
lines are compiled? If you can find the error in my logic then please
help me!!!!
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







