简单的例子:
class Test{
public Test() {
System.out.println("create test ..");
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
new Test();
}
}
}
输出显而易见:
而实现Runnable接口的实现类却出现让我纳闷的情况:
public class SimpleDaemons implements Runnable {
public SimpleDaemons() {
System.out.println("create thread ..");
}
public static void main(String[] args) {
for (int i = 5; i < 5; i++) {
new SimpleDaemons();
}
}
@Override
public void run() {
try {
while (true) {
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(Thread.currentThread() + " " + this);
}
} catch (InterruptedException e) {
System.out.println("sleep() interrupted");
}
}
}
不理解的输出,是没有在控制台上打印无参构造函数中的语句。
疑问:Runnable实现类,创建对象时,不会调用默认的无参构造函数吗?