我有一个简单的Runnable类,它应该遍历世界数据的两个维度,并在每个点执行一个函数。我还有一个布尔值和一个返回方法,它将告诉线程何时完成。
public class Generator implements Runnable
{
private World world;
private Random seed;
private boolean genComplete = false;
public Generator(World world)
{
// Implementation
}
public boolean isComplete()
{
return genComplete;
}
/**
* Begins world generator thread.
*/
@Override
public void run()
{
world.initializeWorldTileData();
generateWholeWorld();
System.out.println("Completed");
genComplete = true;
}
/**
* Processes entire world generation tree.
*/
private void generateWholeWorld()
{
world.saveData();
for (int x = 0; x < world.getWorldSizeX(); x++)
{
for (int y = 0; y < world.getWorldSizeY(); y++)
{
// Example function.
x();
}
}
}
private void x()
{
// When nothing is performed genComplete equals true.
}
}
在此示例下,运行时generateWholeWorld()
方法将完全执行,并打印Completed
。但是,如果我将任何函数添加到x()
:
private void x()
{
System.out.println("Example function");
}
线程保持无限运行(无休止地运行for循环),即使它应该能够在几秒钟内执行任务。 genComplete
永远不等于真。
编辑:正在从GUI加载屏幕创建和观察线程,该屏幕在genComplete
为真时发生变化。
private Thread generator;
private boolean doneGenerating;
public ScreenGenerateWorld(Screen parent, InitialWorldSettings settings)
{
super(parent);
world = World.createNewWorld(settings);
this.generator = new Thread(world.getWorldGenerator());
generator.start();
}
@Override
public void update()
{
doneGenerating = world.getWorldGenerator().isComplete();
if (doneGenerating)
{
info.setText("Press \"A\" to Continue...");
if (Keyboard.isKeyDown(Keyboard.KEY_A))
AntFarm.getAntFarm().changeActiveScreen(new ScreenWorld(parent, world));
}
// Render code
}
答案 0 :(得分:2)
可变状态杀死了多线程Java应用程序。我强烈建议您在genComplete状态变量周围进行一些同步,以便所有线程都具有其值的公共视图。
public class Generator implements Runnable
{
...
private boolean genComplete = false;
public synchronized void setComplete() {
getComplete = true;
}
public synchronized isComplete() {
return genComplete;
}
...
/**
* Begins world generator thread.
*/
@Override
public void run()
{
world.initializeWorldTileData();
generateWholeWorld();
System.out.println("Completed");
setComplete();
}
...
}
答案 1 :(得分:1)
我同意关于不变性的评论,总是首选使用不可变对象, 但我认为同步不是最好的解决方案,
使用“Executors”而不是线程可能会更好。 你可以在Jenkov博客的下一篇简短教程中阅读它:Jenkov
并且您希望将实现接口Callable的实现传递给“call”函数,该接口返回一个“Future”对象,您可以询问该任务是否已完成。
您可以使用“get”阻止功能,等待进程完成。
我希望我能正确地满足您的需求,答案对您有所帮助。
祝你好运!
答案 2 :(得分:1)
这只是一个假设,但似乎你得到了一个带有update()方法的自旋锁,该方法在几次迭代后由JVM优化,以便缓存genComplete,并且自旋锁定线程永远不会确定genComplete&#39;价值改变了。当你的方法x()为空时,方法run()快速完成,JVM还没有优化你的代码(默认情况下,Oracle HotSpot JIT在客户端模式下1500方法调用后打开优化),但I / O操作是1)阻塞(对于其他线程意味着更多的CPU时间)2)不是非常快,因此当x()包含System.out.println()时会涉及优化。 volatile将解决问题。
我建议你使用Executors和callbacks。例如:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button.setEnabled(false);
label.setText("busy");
backgroundExec.execute(new Runnable() {
public void run() {
try {
doBigComputation();
}
finally {
GuiExecutor.instance().execute(new Runnable() {
public void run() {
button.setEnabled(true);
label.setText("idle");
}
});
}
}
});
}
});
更多信息,您可以从Java Concurrency in Practice中学习。