プロジェクト用の「ゲーム エンジン」を作成しようとしていますが、スレッドの問題に直面しています。メイン フローでスレッド (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