简体   繁体   English

如何使Java等待方法完成才能继续?

[英]How do I make Java wait for a method to finish before continuing?

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();

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. 您也可以将同步语句添加到类的方法中,以使它们获取父对象的锁。

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

You can use Thread.sleep(timeInMiliseconds) to pause the currently executing thread. 您可以使用Thread.sleep(timeInMiliseconds)暂停当前​​正在执行的线程。 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. 如果这是用于更新UI的相同线程,则UI将在睡眠期间冻结,因此最好将所有UI内容保留在其自己的线程中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM