I've found strange behavior of java concurrency. See on the next code below:
public class Test { static CountDownLatch latch = new CountDownLatch(1); public static void main(String[] args) throws UnsupportedEncodingException, InterruptedException { final Thread t = new MyThread(); t.start(); synchronized (t) { latch.countDown(); System.out.println("got to sleep"); t.wait(); System.out.println("wake up"); } } static class MyThread extends Thread { @Override public void run() { try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (this) { System.out.println("inside run"); // notifyAll(); } } } }
In my point of view this code should be hang up and wait forever, but code is finished without any problem with next out in console:
got to sleep inside run wake up
I've tried to find some information about notifying locks if thread is died, but was lack in it. Also I've not find any information in java specification.
But if I've tried to lock on some other object (not on the thread object) it was work fine as I expected.