matan
matan

Reputation: 107

Wait until the thread finish

I have a function that has inside thread that do something

public static void animate(int column,Image image)
{

    new Thread(new Runnable() {
        public void run() {
            try {

                    /* Code */
                    repaint();
                    Thread.sleep(500);
                    repaint();

            } catch (InterruptedException ex) {
            }
        }
    }).start();
}

The animation function I summon in the updateBoard function and after this do i++. I want to make the function animate not continue to I++ until the thread end

Inside animate fucntion i used repaint() function from swing, When i try to use .join() its block repaint() thread.

public static void updateBoard(int column, Image image) {
    int i = 0;
    animate(column,image);
    i++;
}

Upvotes: 1

Views: 2801

Answers (2)

nits.kk
nits.kk

Reputation: 5316

You have a already existing thread executing updateBoard() method. Let this thread be T1.
You invoke animate() and create another thread, let this thread be T2. Now as per your question, you want to have T1 not run further until T2 completes the execution.

Thread.java defines a method join(), it allows one thread to wait for the completion of another. In your code it can be done like this.

static Thread animationThread = new Thread(new Runnable() {
    public void run() {
        try {
                /* Code */
                Thread.sleep(500);
        } catch (InterruptedException ex) {}
    }
});
public static void updateBoard(int column, Image image) {
    int i = 0;
    animate(column,image);
    animationThread.join(); // this will make the thread executing updateBoard to wait until the completion of animationThread.
    i++;
}
public static void animate(int column,Image image){
    animationThread .start();
}

But now every thing runs one after the other, there is no use of having two threads in this case. This is similar to

public static void updateBoard(int column, Image image) {
    int i = 0;
    animate(column,image);
    i++;
}
public static void animate(int column,Image image){
     try {
                /* Code */
                Thread.sleep(500);
        } catch (InterruptedException ex) {}
}

In this case also until unless animate method completes (without 2nd thread), i++ will not be executed. Hence for the use case in your question having a separate thread for animate does not make sense, it only adds to the overhead of creating a separate thread and context switching. Although having a separate thread for animation seems a good idea but for that you got to restructure the program you have so as the logic is based on parallel execution so as having multiple threads makes sense.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718678

Like this:

Thread t = new Thread(new Runnable(){...});
t.start();

t.join();

However, this is kind of pointless. If you are going to start a thread and immediately block waiting for it to finish you are NOT going to get any parallelism. You may as well just call the Runnable::run method in the current thread ... and avoid the (not insignificant!) overhead of creating / starting a thread.

Upvotes: 5

Related Questions