Wednesday night puzzle

What’s the result of running this piece of code?

a) Done.
b) InterruptedException is thrown
c) It depends
d) Code doesn’t compile

import java.util.Vector;

public class Main {
  interface A {
  }
  
  static class AImpl implements A {
    static AImpl DUMMY = new BImpl();
  }

  interface B extends A {
  }

  static class BImpl extends AImpl implements B {
  }

  public static void main(String[] args) throws InterruptedException {
    final Vector<Object> values = new Vector<Object>();

    Thread thread = new Thread() {
      @Override
      public void run() {
        values.add(AImpl.DUMMY);
      }
    };
    thread.start();
    values.add(new BImpl());
    thread.join();

    System.out.println("Done.");
  }
}

3 thoughts on “Wednesday night puzzle”

  1. I would said : It depends
    because due to the ugly cyclic dependency between BImpl and AImpl class, there is a kind of dead lock when instantiating them at the same time ?

  2. It depends 🙂

    When the two threads want to load BImpl class at the same time there’s a deadlock and when a thread is faster than the other it works.

Comments are closed.