2016-11-23 09:05:26 -08:00
|
|
|
// concurrent/LambdasAndMethodReferences.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2016-07-05 14:46:09 -06:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-07-05 14:46:09 -06:00
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
class NotRunnable {
|
|
|
|
public void go() {
|
|
|
|
System.out.println("NotRunnable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotCallable {
|
|
|
|
public Integer get() {
|
|
|
|
System.out.println("NotCallable");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class LambdasAndMethodReferences {
|
|
|
|
public static void main(String[] args)
|
|
|
|
throws InterruptedException {
|
|
|
|
ExecutorService exec =
|
|
|
|
Executors.newCachedThreadPool();
|
|
|
|
exec.submit(() -> System.out.println("Lambda1"));
|
|
|
|
exec.submit(new NotRunnable()::go);
|
|
|
|
exec.submit(() -> {
|
|
|
|
System.out.println("Lambda2");
|
|
|
|
return 1;
|
|
|
|
});
|
|
|
|
exec.submit(new NotCallable()::get);
|
|
|
|
exec.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
|
|
|
Lambda1
|
2017-05-10 11:45:39 -06:00
|
|
|
NotCallable
|
2016-07-05 14:46:09 -06:00
|
|
|
NotRunnable
|
|
|
|
Lambda2
|
|
|
|
*/
|