Mam takie klasy:
public class KidRunner {
Kid kid;
public void move() {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
while(!kid.grounded){
kid.moveRandomWay();
System.out.println("Current position : "+ kid.position.x + " , " + kid.position.y );
kid.play();
System.out.println("ZASYIAM " + kid.toString());
Thread.sleep(2000);
}
return null;
}
};
worker.execute();
}
public KidRunner(Kid kid) {
this.kid = kid;
}
}
public class Reminder {
Timer timer;
boolean end = false;
public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
}
class RemindTask extends TimerTask {
public void run() {
end = true;
timer.cancel(); //Terminate the timer thread
}
}
}
//Klasa kid i metody:
public class Kid() {
public Kid() {
grounded = false;
KidRunner kidRunner = new KidRunner(this);
kidRunner.move();
}
public void play() {
Reminder reminder = new Reminder(1);
while( ! reminder.end){
System.out.println("bawie sie");
}
}
I pozniej tworzę sobie kilka takich obiektów klasy Kid np. 10, które teoretycznie powinny wykonać losowy ruch, pobawić się przez 1 sekunde, zasnac na 2 sekundy i ciągle to powtarzać.
W rzeczywistości wygląda to tak, że np. 3 (raz 5, raz 2 ) na 10 obiektów robi to, a reszta stoi w miejscu i nic nie robi. O co może chodzić? Myślałem może, że zapomniałem o jakiejś synchronizacji, ale przecież nie wspóldziele żadnych zasobów?
teez