4

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();
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Asto
  • 41
  • 1
  • 2
  • 4
  • Probably Thread.sleep(), I assume he wants to wait for some period of time – MWB Jan 14 '14 at 22:02
  • 3
    Don't sleep or wait on the UI thread... – assylias Jan 14 '14 at 22:03
  • 1
    Don't use Threads then. – Hauke Ingmar Schmidt Jan 14 '14 at 22:07
  • 1
    It's probably best to have your separate threads check conditions have been met before proceeding. Basically "check in" with your controller and see if they should proceed or loop until given the OK. I would avoid `Thread.sleep()` in all but test/throw-away code. `Thread.sleep()` has all kinds of problems including not being precise. If you need precise firing of methods, it may be worth to check into `ScheduledExecutorService` and/or the `Quartz` library. – SnakeDoc Jan 14 '14 at 22:10
  • Why are you using `Thread`s at all? Do you want to keep the operations out of the EDT but in sequential order? Then you need _one_ `Thread`, not one _per operation_ as you do now. – Hauke Ingmar Schmidt Jan 15 '14 at 10:55
  • Hi! Have you resolved that problem? I guess that I'm facing the same one. Do you have Timer there? I assume that you are using the JFrame with the JPanel and repainting everything while Timer's running. Am I right? – Aidos Sep 13 '16 at 10:10

2 Answers2

9

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

Anedar
  • 4,235
  • 1
  • 23
  • 41
  • Would be worth to make some demonstration about your solution. Just thinking about people that came here, and could probably be waiting for a more "strong" answer. – ivanleoncz Nov 16 '16 at 20:23
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.

Mike B
  • 5,390
  • 2
  • 23
  • 45
  • 5
    It's also best to not use `Thread.sleep()` anymore. It's old and has been layered over with much better threading abstraction layers such as `java.util.concurrent.*`. Threads should "check in" with the controller to determine flow. – SnakeDoc Jan 14 '14 at 22:15