Wait, notify, notifyAll não devem ser usados somente em contextos sincronizados?

1 resposta
D

wait, notify, notifyAll não devem ser usados somente em contextos sincronizados??
Então pq este codigo abaixo compila e executa??

public class TwoThreads {
 static Thread laurel,hardy;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
   laurel=new Thread(){
	   public void run(){
	System.out.println("A");
	try {
		hardy.sleep(1000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		System.out.println("B");
	}
	   
	 System.out.println("C");  
	   
	   
   } } ;
	
   hardy=new Thread(){
	   public void run(){
	System.out.println("D");
	try {
		laurel.wait();//Não está no contexto sincronizado na minha opinião.
		} catch (Exception e) {
		// TODO Auto-generated catch block
		System.out.println("E");
	}
	   
	 System.out.println("F");  
	   
	   
   } } ;
	
	laurel.start();
	hardy.start();
	}

}

Agradeço a atenção de todos.

1 Resposta

Leafar

Opa,

o problema não é em tempo de compilação,
mas em tempo de execução. Se você notar
o try/catch da thread “hardy”, ele lança uma
IllegalMonitorStateException. =)

flws

Criado 10 de março de 2009
Ultima resposta 11 de mar. de 2009
Respostas 1
Participantes 2