I found the weird behavior of java concurrency. See the following 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 ();
}
}
}
}
From my point of view, this code should be hung up and wait forever, but the code ends without any problems with the following in the console:
got to sleep
inside run
wake up
I tried to find some information about the notification of locks if the thread died, but it was missing. Also, I did not find any information in the java specification.
But if I tried to block some other object (and not a thread object), it worked fine, as I expected.
source
share