起初,我这样做:在Java中,为什么我的多线程不工作?
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完全初始化。
我做错了什么,我该如何解决这个问题?
我会尽快尝试这个,谢谢。 – striking
你也可以使用'for(Iterator i = players.iterator(); i.hasNext();)i.next()。update(fcount);' –
OldCurmudgeon