In Concurrency Interest link, there is a code which is like this:-
exec.schedule( ()-> System.out.println("done"),
1, TimeUnit.SECONDS );
What is the meaning of ()-> ?
I checked in eclipse, it does not allow. But what was the intention of the thread-writer?
This is Lambda syntax from JDK8.
It is pretty similar (but not exactly same) to
exec.schedule(new Runnable() {
public void run() {
System.out.println("done");
}
}, 1, TimeUnit.SECONDS);
That is the Java 8 syntax for Lambda Expressions.
The ScheduledThreadPoolExecutor#exec(..)
method expects a Runnable
argument. Runnable
is a functional interface because it only contains one abstract
method. As such, the compiler can infer that you are defining a new Runnable
instance with the lambda.
The parts between ()
are the run()
method's parameters, ie. none. The part after the ->
is the body of the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With