Re: TimerTask not work as expected
SamuelXiao wrote:
Hi all, I am writing a simple monopoly board game, there're only 2
tokens, one is controlled by human, another by PC. I am trying to
make it turn based and move around the map step by step. Then when I
use TimerTask to trigger the step-forward movement, it's ok for the
token controlled by human, but for the PC one. It doesn't move as
expected. Below is part of the codes for Dice Roll button& Done
button.
timer.schedule(new TimerTask(){
Don't use TAB characters to indent Usenet posts; use spaces (up to four per
level).
private int temp = Dice1 + Dice2;
You have not synchronized access do 'Dice1' (variable names should start with
a lower-case letter) or 'Dice2'.
....
how could I make sure that btnRoll() is done then go to the next
code?
Read up on 'volatile', 'synchronized' and other concurrent-programming
constructs in Java. Buy, read and study /Java Concurrency in Practice/ by
Brian Goetz, et al.
Any help would be appreciated.
Travers Naran wrote:
Timer runs TimerTask in a _separate_ thread. There are a few ways you
could synchronize this, but I'd recommend looking at wait()/notify().
Try to remember that you are waiting for your TimerTask to be called
Dice1+Dice2 times before you leave.
SamuelXiao wrote:
Thanks for youjr suggestion, I use wait() now, but it comes to another
problem. I added wait() in the AITurn() method..Then now an exception
was caught..
public void AIturn(int tempNumOfPlayers){
long temp = (long) (Dice1 + Dice2) * 100;
btnRoll();
SystemLogHelper.info("players.get(turn-1): " +
players.get(turn-1).getName());
SystemLogHelper.info("players.get(turn-1).getPosition(): " +
players.get(turn-1).getPosition());
try {
this.wait(temp);
Putting 'this.' in front of method calls is useless and misleading.
// notify();
// Thread.sleep(temp);
}
catch(InterruptedException e){}
if(propertymanager.Properties[players.get(turn-1).getPosition()]
[0] == 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
btnDone(tempNumOfPlayers);
// }
// if(rolled) btnDone(tempNumOfPlayers);
}
// IllegalMonitorStateException
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at com.xxx.applet.MonopolyBoard.AIturn(MonopolyBoard.java:822)
at com.xxx.applet.MonopolyBoard.btnDone(MonopolyBoard.java:485)
That exception is thrown when you call 'wait()' without having a "monitor"
(lock) held on the 'this' object. This is clearly stated in the Javadocs for
the method. Did you read them?
http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#wait(long)
"The current thread must own this object's monitor."
You have to synchronize access to data shared between threads. This is a
rather large topic.
Read up on concurrent programming. Measure twice, cut once.
--
Lew
Ceci n'est pas une pipe.