Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4393

Waiting till previous function is completely executed

I have a series of IF statements in order of execution, my dilemma is if one of the IF statements is entered I would like the calling method to wait till the called function is finished. The thing is my called function is recursive so I have no idea when it's going to end. Here's the calling method's code.

                if(log.isChecked())
                {
                    runlog(sdPath.getAbsolutePath());
                }
                if(tmp.isChecked())
                {
                    runtmp(sdPath.getAbsolutePath());
                }
                if(txt.isChecked())
                {
                    runtxt(sdPath.getAbsolutePath());
                }
                if(bf.isChecked())
                {
                    runbf(sdPath.getAbsolutePath());
                }
                if(ed.isChecked())
                {
                    runed(sdPath.getAbsolutePath());
                }

If log.isChecked() is entered, I would like the calling function(the code I've shown here, that function) to wait till it checks the next condition which is tmp.isChecked()

As I mentioned before ALL the called functions runlog, runtmp, runtxt, runbf, runed are recursive. How do I achieve what I want?

Upvotes: 0

Views: 1269

Answers (2)

Andrew Campbell
Andrew Campbell

Reputation: 18675

I think recursion can is best explained with the movie "Inception". You have 3 things you want to do.

  1. Go to sleep.
  2. Dream.
  3. Wake up and eat a bowl of cereal.

Imagine you are dreaming and then have a dream within that dream. You now can't wake up and eat until you exit out of the deeper dream and finish the current dream.

That is basically what is happening in your example except instead of dreaming you ran the first if and you can't exit it until it reaches its return condition then you get to wake up and eat (enter the second if).

Upvotes: 2

Anup Cowkur
Anup Cowkur

Reputation: 20563

This should happen by default. The nested functions being recursive makes no difference here.

Each if block will only be executed after one the previous one has completed unless your nested functions are starting threads or something of that sort.

Upvotes: 1

Related Questions