Java 8 Lambdas limitations: closures
Suppose we want to create a simple thread that only prints something on the console:
int answer = 42; Thread t = new Thread( () -> System.out.println("The answer is: " + answer) );
What if we changed the value of
answer while the thread is executing?
In this article, I would like to answer this question, discussing the limitations of Java lambda expressions and consequences along the way.
The short answer is that Java implements closures, but there are limitations when we compare them with other languages. On the other hand, these limitations can be considered negligible.