I would expect the following code to print out on the first line: initial value.
public class RunnableLambda {
static String accessedByThreads = "initial value";
public static void main(String... args) {
Runnable r8 = () -> {
try {
Thread.currentThread().sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("r8");
accessedByThreads = "from runnable lambda";
};
r8.run();
Runnable r = new Runnable() {
@Override
public void run() {
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("r");
accessedByThreads = "from runnable anonymous";
}
};
r.run();
System.out.println("Main");
System.out.println(accessedByThreads);
}
}
Because I would expect spawned threads to finish after the main. However, it prints out on the last line: from runnable anonymous.
Why?