private ExecutorService exec = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r);
t.setDaemon(true); // allows app to exit if tasks are running
return t ;
});
我理解执行者背后的想法,但参数r
令我困惑。我用:难以理解Java 8 Lambda
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;
});
深入挖掘,其结果是:
Class of r: class java.util.concurrent.ThreadPoolExecutor$Worker. r to string: [email protected][State = -1, empty queue]
Class of t: class java.lang.Thread. Name of t: Thread-3
r
被作为一个参数对Thread
对象构造通过。
- 简单字母
r
如何表示传递的对象是ThreadPoolExecutor
? - 如果
ThreadPoolExecutor
不按Thread's
构造函数的要求执行Runnable
,ThreadPoolExecutor
如何作为参数通过?
如果有人能为我提供非lambda版本的代码,那对我的理解会有很大的好处。
谢谢,Radiodef。我现在完全理解代码。我在哪里可以了解更多关于'outerclass $ innerclass'类型的记法 - Java语言规范? – user465001 2014-11-06 03:40:31
我很高兴解释它。学习'$'也没有更多。当您在代码中引用嵌套类或内部类时使用'Outer.Inner',但通常将其编译为名为'Outer $ Inner'的类。 '$'是所有标识符的有效字符,但通常认为它是为内部机制保留的。 http://stackoverflow.com/q/7484210/2891664 – Radiodef 2014-11-06 03:44:38
再次感谢Radiodef! – user465001 2014-11-06 03:58:28