1

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++;
}
matan
  • 97
  • 13
  • Does this help: https://stackoverflow.com/questions/289434/how-to-make-a-java-thread-wait-for-another-threads-output ? – Oozeerally Apr 30 '20 at 10:20
  • If you want it synchronous why are you using threads? – user207421 Apr 30 '20 at 10:24
  • Can you yourself make out what do you mean by the sentence : "I use Thread and i want make that thread cannot continue to next line until he finish the thread." There is a already Running thread T1 (it may be the main thread or may be some other). Then there is a new thread T2 ... edit the question to be clear. Questions are great references for the future readers so take a moment to make your question's understandability better. Its is relevant for you now , in future it will be helpful for others. Stackoverflow is not a personal tutor but its for the community by the community. – nits.kk Apr 30 '20 at 10:38
  • @user207421 Because i want to make delay between two lines – matan Apr 30 '20 at 11:54

2 Answers2

5

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • Its not work.. maybe because I use within the animation function - `repaint()` to change images and when i write `animationThread.join()` block repaint thread – matan May 02 '20 at 15:28
  • add those details in the question to make it clear. Clearly mention what is being done in the functions and also you got to undertake as how threads work. I really could not make out anything when you say "block the repaint thread", is there a method repaint which creates another thread, if so how is it blocked, are you talking about the thread T1 ? Usually there is a main UI thread, then there are other threads, please also mention if this is related to android, swing, java fx or what. Tag the context for the question. – nits.kk May 02 '20 at 17:47
  • You're right, my mistake. I updated the question again and I mean swing – matan May 03 '20 at 07:48
  • what is the role of `i` ? can you make it part of the thread defined in animate itself ? what is the advantage you are seeking by defining the thread ? If your further execution can not proceed ahead , what is the purpose of having the separate thread ? – nits.kk May 03 '20 at 18:12
  • I want `i` will be executed after i finish the thread. I use thread because i want to make delay in `animate` function when i changing images inside the function – matan May 03 '20 at 18:58
  • @matan Ok now figure out how many threads are you dealing with. One is animationThread, now Find out in which thread updateBoard is executed, Does repaint also executes in same thread ? If this is the case then there is a big mistake in the logic of your program. You in this case wants the same thread to be in waiting state and in side your animationThread you are tring to invoke repaint, with this approach you only get a Deadlock and nothing else. – nits.kk May 03 '20 at 19:34
  • @matan did you resolved your issue or understood where it is going wrong ? – nits.kk May 05 '20 at 07:14
  • No my friend, I gave it up – matan May 05 '20 at 22:30
  • @matan do not give it up. Learn about threading in java and restructure your program logic. You are trying hard to fight a war without knowing how to use the tools and hence you are facing issues which are hard to resolve. Once you learn the basics, you will approach the program with a new view which will be simple to code and in case of doubts you will ask correct questions. – nits.kk May 05 '20 at 23:16