简体   繁体   中英

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 ThreadPoolExecutor passable 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.

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 which is a private inner class of ThreadPoolExecutor that implements Runnable. ,它是一个实现Runnable的ThreadPoolExecutor的私有内部类。

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM