我正在尝试为我的项目编写一些“游戏引擎”,但我遇到了线程问题。当我在主流中创建一个线程(LoadThread)时,它一直等到 Run(); 在 LoadThread 结束。
//loading thread
public class LoadThread implements Runnable{
private boolean running = false;
private Thread loader = null;
public LoadThread(/*init data structures*/){
loader = new Thread(this);
}
public void start(){
running = true;
run();
}
synchronized public void run() {
System.out.println(" loading started ");
while(running){
//do some loading, when done, running = false
}
System.out.println(" loading done ");
}
}
//holds data, starts loading
public class SourceGod {
private LoadThread img_loader;
public void startLoading(){
img_loader = new LoadThread(/* some data structures */);
img_loader.start();
}
}
//runs the game
public class Game extends GameThread implements ActionListener{
private SourceGod sources;
public Game(Window full_screen){
sources = new SourceGod(/* some data structures */);
System.out.println("before");
sources.startLoading();
System.out.println("after");
}
}
//own thread to refresh
abstract public class GameThread extends JPanel implements Runnable{
//anything from there is not called before "after"
}
输出
before
loading started
//some loaded data report, takes about 2-3s
loading done
after
任何帮助将不胜感激。(更多代码http://paste.pocoo.org/show/orCfn9a8yOeEQHiUrgjG/)谢谢,Vojtěch