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.
- How is the simple letter
r
indicating that the object being passed is aThreadPoolExecutor
? - How is a
ThreadPoolExecutor
passable as a parameter if it does not implementRunnable
as required by the byThread'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.