I am trying to find out the cause behind below mentioned code. Here if I create Thread using anonymous inner class it goes into deadlock state but with lambda expressions it works fine. I tried to find the reason behind this behavior but I could not.
public class ThreadCreationTest {
static {
new ThreadCreationTest();
}
private void call() {
System.out.println("Hello guys!!!");
}
public ThreadCreationTest() {
// when we use this thread it goes in deadlock kind of state
Thread thread1 = new Thread(new Runnable() {
public void run() {
call();
}
});
// This one works fine.
Thread thread = new Thread(() -> call());
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public static void main(String... args) {
System.out.println("Code finished...");
}
}
with lambda expression output :
Hello guys!!!
Code finished...
with anonymous class :
code goes into deadlock state