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

我的未来我决定 提交于 2019-12-31 03:19:07

问题


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

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/21124879/how-do-i-make-java-wait-for-a-method-to-finish-before-continuing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!