Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having difficulty understanding a Java 8 Lambda

private ExecutorService exec = Executors.newSingleThreadExecutor(r -> {
    Thread t = new Thread(r);
    t.setDaemon(true); // allows app to exit if tasks are running
    return t ;
});

I understand the idea behind an executor but, the paramater r is confusing me. I used:

 final ExecutorService exec = Executors.newSingleThreadExecutor(r -> {
        Thread t = new Thread(r);
        System.out.println("Class of r: " + r.getClass()+ ". r to string: " + r.toString());
        System.out.println("Class of t: " + t.getClass() +". Name of t: "+ t.getName());
        t.setDaemon(true);
        return t;
    });

to dig deeper and the result is:

Class of r: class java.util.concurrent.ThreadPoolExecutor$Worker. r to string: java.util.concurrent.ThreadPoolExecutor$Worker@1dc3963[State = -1, empty queue]
Class of t: class java.lang.Thread. Name of t: Thread-3

r is being passed as a parameter to the Thread object constructor.

  1. How is the simple letter r indicating that the object being passed is a ThreadPoolExecutor?
  2. How is a ThreadPoolExecutorpassable as a parameter if it does not implement Runnable as required by the by Thread's constructor?

If someone could provide me with a non-lambda version of the code as well, it would be of great benefit to my understanding.

like image 700
user465001 Avatar asked Nov 06 '14 03:11

user465001


People also ask

Why do you think lambda expressions are considered such a big thing for Java 8?

Introduction. Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .

How do you pass lambda in Java?

A lambda expression is an anonymous or unnamed method in Java. It doesn't execute on its own and used to implement methods that are declared in a functional interface. If we want to pass a lambda expression as a method parameter in java, the type of method parameter that receives must be of functional interface type.

Which is correct about Java 8 lambda expression?

Q 5 - Which of the following is correct about Java 8 lambda expression? A - Lambda expressions are used primarily to define inline implementation of a functional interface.

Does Java 8 have lambda?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.


1 Answers

newSingleThreadExecutor takes a ThreadFactory as an argument. ThreadFactory defines a single method newThread that takes a Runnable as an argument and returns a Thread.

The lambda may make more sense to you if we specify the type of r explicitly:

(Runnable r) -> {
    Thread t = new Thread(r);
    return t;
}

Now it is more obvious this is a definition for the body of newThread.

Except that since the lambda is immediately passed as an argument to a method that accepts a ThreadFactory, the compiler is able to infer that the type of r must be Runnable. Therefore it can be omitted.

Without the lambda, this code translates to the following anonymous class definition and instantiation:

private ExecutorService exec = Executors.newSingleThreadExecutor(
    new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    }
);

How is the simple letter r indicating that the object being passed is a ThreadPoolExecutor?

The type of r is Runnable because the target type of the lambda expression defines its single method that way.

The object you are seeing passed is actually a ThreadPoolExecutor$Worker which is a private inner class of ThreadPoolExecutor that implements Runnable.

How is a ThreadPoolExecutor passable as a parameter if it does not implement Runnable as required by the by Thread's constructor?

See above (r is a Runnable).

like image 89
Radiodef Avatar answered Oct 15 '22 08:10

Radiodef