如何让 Java 在继续之前等待方法完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21124879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make Java wait for a method to finish before continuing?
提问by Asto
So my problem is that I need these methods to run one after another but I cannot work out how to make the methods wait before being run. Any help is appreciated. Thank you. Here is my code:
所以我的问题是我需要这些方法一个接一个地运行,但我无法弄清楚如何让这些方法在运行之前等待。任何帮助表示赞赏。谢谢你。这是我的代码:
public void startMoving() throws InterruptedException
{
moveEnemy("right",3);
wait();
moveEnemy("down",3);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",2);
wait();
moveEnemy("down",4);
wait();
moveEnemy("left",1);
wait();
moveEnemy("down",2);
wait();
moveEnemy("right",3);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",1);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",3);
}
public void moveEnemy(final String direction, final int numMoves)
{
Thread moveThread = new Thread(new Runnable()
{
public void run()
{
isMoving = true;
int originalX = getX();
int originalY = getY();
for(int loop = 0; loop <= 98*numMoves; loop++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e){}
if(direction.equals("up"))
{
setLocation(originalX,originalY+loop);
}
if(direction.equals("down"))
{
setLocation(originalX,originalY-loop);
}
if(direction.equals("left"))
{
setLocation(originalX-loop,originalY);
}
if(direction.equals("right"))
{
setLocation(originalX+loop,originalY);
}
}
try
{
Thread.sleep(50);
}
catch (InterruptedException e){}
notify();
}
});
moveThread.start();
回答by Mike B
You can use Thread.sleep(timeInMiliseconds)
to pause the currently executing thread. If this is the same thread you use to update your UI then the UI will be frozen during the sleep so it's best to keep all the UI stuff on its own thread.
您可以Thread.sleep(timeInMiliseconds)
用来暂停当前正在执行的线程。如果这是您用来更新 UI 的同一线程,则 UI 将在睡眠期间冻结,因此最好将所有 UI 内容保留在其自己的线程上。
回答by Anedar
The easiest solution might be to not use threads, but i doubt that is what you want.
最简单的解决方案可能是不使用线程,但我怀疑那是您想要的。
What you might be looking for is the concept of locks:
您可能正在寻找的是锁的概念:
A method may acquire the lock associated with an object by calling:
方法可以通过调用获取与对象关联的锁:
synchronized(nameOfTheLockObject) {
//do some code here
}
This acquires the lock of the given Object, executes the code and releases the lock afterwards. If the lock is already acquired by another method/thread, the code pauses until the lock is released by the other method/thread.
这获取给定对象的锁,执行代码并随后释放锁。如果锁已经被另一个方法/线程获取,代码会暂停,直到锁被另一个方法/线程释放。
You can also add the synchronized statement to methods of a class to make them acquire the lock of the parent object.
您还可以在类的方法中添加synchronized 语句,使其获取父对象的锁。
More Information on this is given at: http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
有关这方面的更多信息,请访问:http: //docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html