0

我有一个方法

public static void startAnimation() {
    new AnimationThread().run();
}

其中AnimationThread实现了runnable,它的构造函数是:

public AnimationThread() {
    new Thread(this, "Animation Thread");
    EventQueue.setAnimationCounter(0);
    alive = true;
}

我从小程序的 init() 方法调用它挂起,因为它从不返回值。有没有办法启动这个线程并让 init() 方法完成,以便我的小程序启动!

谢谢

4

2 回答 2

4

您需要稍微移动一下:

public AnimationThread() {
   EventQueue.setAnimationCounter(0);
   alive = true;
   new Thread(this, "Animation Thread").start();
}

public static void startAnimation() {
   new AnimationThread();
}

start()Thread是在不同线程上运行代码的神奇方法;AnimationThread构造函数调用后会正常返回,会AnimationThread.run()在新线程中执行。

于 2011-12-09T12:14:40.773 回答
2

也许你应该调用start方法而不是run方法。只有start方法真正执行一个新线程。

于 2011-12-09T12:13:32.057 回答