I have written a class in Java:
public class Main {
    public static boolean firstRunning = true;
    public static void main(String[] args) {
        (new Thread(){
            public void run(){
                secondFunction();
            }
        }).start();
        firstFunction();
    }
    public static void firstFunction()
    {
        for(int i = 0; i < 10 && firstRunning; i++)
        {
            try{Thread.sleep(1000);} catch(Exception e){}
            System.out.println("first - "+i);
        }
        return;
    }
    public static void secondFunction(){
        try{Thread.sleep(3000);} catch(Exception e){}
        firstRunning = false;
        for(int i = 0; i < 10; i++)
        {
            try{Thread.sleep(700);} catch(Exception e){}
            System.out.println("second - "+i);
        }
    }
}
I am calling the secondFuntion() in a new Thread after which I begin the execution of firstFuntion() in the Main Thread. After the execution of the Thread.sleep(3000) in the secondFuntion(), I want to make secondFunction() take over the Main Thread, for which I set the boolean flag firstRunning to false and cancel the execution of the Main Thread using the return statement.
This approach works for me, but is there a more elegant way of doing the same?
EDIT
The output of the code is
first - 0
first - 1
first - 2
second - 0
second - 1
second - 2
second - 3
second - 4
second - 5
second - 6
second - 7
second - 8
second - 9
 
     
     
     
    