views:

68

answers:

1

So I have a TimerTask task calling a function onTimerComplete() in its run()

onTimerComplete() looks something like this:

private void onTimerComplete(){
  myFunc1();
  myFunc2();
}

I make a Timer t and I schedule the TimerTask with t.schedule(task, 2000);

The problem is, when the timer is up and the task runs my onTimerComplete() but that function does not finish. It runs myFunc1() but never finishes it nor does it ever call myFunc2()

However, if I call onTimerComplete() directly, everything works.

What's the deal here?

+2  A: 

If myFunc1() starts, but never finishes, then it's most likely that the problem is in that function.

You need to be aware that this function will be called in a separate thread. There is the possibility that there is some kind of unwanted interaction between two threads. (Description here.)

If myFunc1() uses some variables that are also used in other concurrent activities, you may need to synchronize parts of your code. The concurrency tutorial might help you work out what the problem is, and how to fix it.

John
Thanks for the info. Upon further reading/testing it's definitely a threading issue. Thanks for pointing me in the right direction!
Matt Swanson