Leem.fin
Leem.fin

Reputation: 42592

multithreading : wait for another thread in main thread

I have three functions funcOne() , funcTwo() & funcThree() which are invoked one by one in main thread:

public static void main(String[] args) {
    funcOne();
    funcTwo();
    funcThree();
}

I want the three functions be running in the above order. funcOne() and funcThree() is fine, since they are running on main thread. For funcTwo(), it's task is running in anther thread:

public static void funcTwo(){
   Thread thread = new Thread(){

        @Override
    public void run(){
           System.out.println("function two is running.");
        }
   }
   thread.start();
}

When I run my main function, I see funcTwo() runs after funcThree() . How can I make sure funcTwo() runs between funcOne()? & funcThree() ?

Upvotes: 0

Views: 99

Answers (5)

Mak
Mak

Reputation: 616

Use Countdownlatch:

public class MyTestClass {
    static final CountDownLatch latch = new CountDownLatch(1);
    public static void main(String[] args) {

        funcOne();
        funcTwo();
        try { latch.await(); } catch (InterruptedException e) {}
        funcThree();
    }

    public static void funcOne() {
        System.out.println("function one ran");
    }

    public static void funcTwo(){
        Thread thread = new Thread(){
            @Override public void run(){
                System.out.println("function two ran.");
                latch.countDown();
            }
        };
        thread.start();
    }

    private static void funcThree() {
        System.out.println("function three ran");
    }
}

Upvotes: 1

Crizly
Crizly

Reputation: 1009

funcOne();
Thread thread = funcTwo();
thread.Join();
funcThree();

This will execute the threads in order, when you call thread.join() it'll wait for the thread to finish, although this will freeze up your GUI or any other processes while it finishes if the thread takes some time.

What do the threads do?

Upvotes: 1

Harmlezz
Harmlezz

Reputation: 8068

You may try this:

public static void main(String[] args) {
    funcOne();
    funcTwo();
    funcThree();
}

public static void funcOne() {
    System.out.println("function one ran");
}

public static void funcTwo(){
    Thread thread = new Thread(){
         @Override public void run(){
            System.out.println("function two ran.");
         }
    };
    thread.start();
    try { thread.join(); } catch (InterruptedException e) {}
 }    

private static void funcThree() {
    System.out.println("function three ran");
}

Upvotes: 1

Bitman
Bitman

Reputation: 2006

return created thread object from funcTwo and use thread.join() after funcThree

Or use CountDownLatch if you have more then one threads.

Upvotes: 0

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

I don't know what you want to do, but, I think, is answer to your question.

public static Thread funcTwo(){
   Thread thread = new Thread(){

        @Override
    public void run(){
           System.out.println("function two is running.");
        }
   }
   thread.start();
   return thread;
}

funcOne();
Thread thread = funcTwo();
thread.Join();
funcThree();

Upvotes: 0

Related Questions