In Java I have a program similar to as follows:
public void asyncLambda(Notifier notifier)
{
DataUtil.getDataAsync((data) -> {
// This code would be called on a new thread.
for(Object o : data)
{
notifier.notify(o);
}
});
}
This compiles and runs (as far as I can tell). What I am curious about is what notifier
object I am using.
The possibilities that I can think of are
- A reference to
notifier
at the time of the original call is passed into the lambda. - When the lambda code is executed it gets moved back into the current stack frame of the function, even if the function is being called by a different caller (thereby meaning that notifier is now different).
- The entire stack frame is copied into the new thread (this is sort of similar to the first possibility, but now all variables that may exist are also over there).