起初我是这样做的:
public SpaceCanvas(){
new Thread(new Runnable () {//this is the thread that triggers updates, no kidding
int fcount = 0;
@Override
public void run() {
System.out.println("Update thread started!");
while(!Thread.interrupted()){
fcount++;
while(players.iterator().hasNext()){
players.iterator().next().update(fcount);
}
while(entities.iterator().hasNext()){
entities.iterator().next().update(fcount);
}
System.out.println("About to paint");
repaint();
System.out.println("Done with paints");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
players.add(new LocalPlayer(0, 9001, 0, 0, 0, 0, this, null));
}
在我称之为 SpaceCanvas 的东西的初始化程序中。但是,这不允许创建画布,因此也不允许创建它所在的小程序,因为线程实际上并没有异步运行。然后,我用“.run()”替换了“.start()”,线程只运行了一次,但 SpaceCanvas 完美初始化。
我做错了什么,我该如何解决?