-1

I had tried with the following code and it seems to be a deadlock scenario

public class MyThread {

    public static void main(String[] args) {
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

but i doubt this is not the case. Its not an deadlock. What I understood is the Main thread is waiting for its own completion. I had dig into it but the wait method inside join is native and I'm unable to get more info over it.

mtk
  • 13,221
  • 16
  • 72
  • 112

1 Answers1

2

From Wikipedia

"A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does."

In this case, you are waiting for a thread to finish, and that thread is waiting for a task to complete. While you only have one thread there is still a deadlock IMHO.

FYI see if you can spot the deadlock in this code.

public class Main {
    static String HI = "Hello World";

    static {
        Thread t = new Thread() {
            @Override
            public void run() {
                System.out.println(HI);
            }
        };
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String... args) {
    }
}

BTW If you make HI final it doesn't deadlock. ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130