¿La sincronización de Java está haciendo la notificación automática al salir? ¿Se espera esto?

Pregunta de Java:

Al salir del bloque de sincronización se notifica automáticamente a All (). ¿Es ese el comportamiento esperado?

Lo he probado y parece que 1. cuando una ejecución sale del bloque de sincronización, hace un notificador automático () 2. Cuando el método en sí está sincronizado, hace un aviso automático () cuando regresa. (No a notificar a Todo ())

Código:

<pre><code>public class Test { public static void main(String[] args) throws InterruptedException { MyThread lock = new MyThread(); new WatingThread(lock,1).start(); new WatingThread(lock,2).start(); //above tow threads would start and then wait for a lock lock.start(); } } class MyThread extends Thread { public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("MyThread is trying to acquire the lock .."); synchronized (this) { System.out.println("MyThread has acquired the lock !!"); System.out.println("MyThread is Coming out of synch block.."); } System.out.println("MyThread has released the lock !!"); } } class WatingThread extends Thread { private Object lock; private int id; public WatingThread(Object lock, int id ) { this.lock = lock; this.id = id; } @Override public void run() { System.out.println(String.format("[%d] : Check if lock is available ...",new Object[]{id})); synchronized (lock) { System.out.println(String.format("[%d] : Acquired the lock !!",new Object[]{id})); try { System.out.println(String.format("[%d] : Going to wait on lock.. ",new Object[]{id})); lock.wait(); System.out.println(String.format("[%d] : Got notified !!!",new Object[]{id})); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(String.format("[%d] :I am done !!",new Object[]{id})); } } } </code></pre>

Salida:

[2]: compruebe si el bloqueo está disponible ...
[2]: Adquirió la cerradura !!
[1]: compruebe si el bloqueo está disponible ...
[2]: Vamos a esperar en la cerradura ..
[1]: Adquirió la cerradura !!
[1]: Voy a esperar en la cerradura ..
MyThread está tratando de adquirir el bloqueo ..
MyThread ha adquirido la cerradura !!
MyThread está saliendo de bloque de sincronización ...
MyThread ha liberado la cerradura !!
[1]: ¡¡¡Me notificaron !!!
[1]: ¡Ya terminé!
[2]: ¡¡¡Me avisaron !!!
[2]: ¡Estoy listo!

Respuestas a la pregunta(2)

Su respuesta a la pregunta