问题 1,代码 1 会打印出来" hello world" 吗,为什么? 问题 2,代码 2 会打印出来"hello world " 吗,为什么?
//代码 1
public class Main {
static ExecutorService service = Executors.newSingleThreadExecutor();
public static void main (String[] args) {
service.execute(()->{
while (true){
hello2();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
private static void hello2() {
hello();
}
private static void hello() {
service.submit(new Runnable() {
@Override
public void run() {
hello2();
System.out.println("hello world");
}
});
}
}
//代码 2
public class Main {
static ExecutorService service = Executors.newSingleThreadExecutor();
public static void main (String[] args) {
service.execute(new Runnable() {
@Override
public void run() {
hello2();
}
});
}
private static void hello2() {
hello();
}
private static void hello() {
service.submit(new Runnable() {
@Override
public void run() {
hello2();
System.out.println("hello world");
}
});
}
}