25

I am looking at the following Stack Overflow answer: How to change Spring's @Scheduled fixedDelay at runtime

And in the code there is the following line:

schedulerFuture = taskScheduler.schedule(() -> { }, this);

I would like to know what the lambda () -> {} means in that code. I need to write it without using lambdas.

Boann
  • 48,794
  • 16
  • 117
  • 146
Aliuk
  • 1,249
  • 2
  • 17
  • 32
  • 29
    It's a `Runnable` that does nothing. – ernest_k Sep 12 '18 at 10:25
  • first argument is runnable, so you are passing anonymous class of instance Runnable in which run method does nothing i.e. is empty. – SMA Sep 12 '18 at 10:27
  • 4
    without lambda, you would need to pass an anonymous Runnable with empty body, like `new Runnable(){ @Override public void run(){} }` – Alex Salauyou Sep 12 '18 at 10:31

4 Answers4

43

Its a Runnable with an empty run definition. The anonymous class representation of this would be:

new Runnable() {
     @Override public void run() {
          // could have done something here
     }
}
dun
  • 49
  • 6
Naman
  • 27,789
  • 26
  • 218
  • 353
  • 8
    Related: https://stackoverflow.com/a/26553481/1076640 (contains commentary on why the idiom is `() -> {}` and not `Runnable.noop`, from one of the Java designers). – yshavit Sep 12 '18 at 20:43
2

Lamda expression is an anonymous function that allows you to pass methods as arguments or simply, a mechanism that helps you remove a lot of boilerplate code. They have no access modifier(private, public or protected), no return type declaration and no name.

Lets take a look at this example.

(int a, int b) -> {return a > b}

In your case, you can do something like below:

schedulerFuture = taskScheduler.schedule(new Runnable() {
     @Override 
     public void run() {
        // task details
     }
}, this);
1

For lambdas:

Left side is arguments, what you take. Enclosed in () are all the arguments this function takes

-> indicates that it's a function that takes what's on the left and passes it on to the right for processing

Right side is the body - what the lambda does. Enclosed in {} is everything this function does

After you figure that out you only need to know that that construction passes an instance of matching class (look at what's the expected argument type in the schedule() call) with it's only method doing exactly the same as the lambda expression we've just analyzed.

Deltharis
  • 2,320
  • 1
  • 18
  • 29
0

Lambda expressions basically express instances of functional interfaces. In a way Lambda expression will be: (lambda operator params) -> {body}

() -> System.out.println("This means Lambda expression is not taking any parameters");

(p) -> System.out.println("Lambda expression with one parameter: " + p);

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Archit Sud
  • 129
  • 8
  • This doesn't answer the question - why the empty lambda. This is just a general explainer of what lambdas are. – Andrejs Sep 12 '18 at 18:19
  • @Andrejs, why you have down, voted my answer. Did you see what user has asked here? Let me repeat it for you. Here is the last line of the question "I would like to know what the lambda () -> {} means in that code. I need to write it without using lambdas." He as asked for the meaning of () -> {} – Archit Sud Sep 13 '18 at 05:41