Thread

Quando um objeto chama o método wait(), o Thread que estiver com o bloqueio deste objeto libera o seu bloqueio para outros threads acessarem o codigo sincronizado?

[quote=“javadoc do wait”]Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

The current thread must own this object’s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object’s monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution. [/quote]

[quote=“javadoc do método wait(long timeout)”]Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

The current thread must own this object’s monitor.

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. [/quote]

[quote=ViniGodoy][quote=“javadoc do método wait(long timeout)”]Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

The current thread must own this object’s monitor.

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. [/quote][/quote]

Usei o google tradutor aqui, mas ta bravo a tradução rs. Mas pelo que vi ele libera o bloqueio para outros threads acessarem o codigo sincronizado certo?

Sim, libera sim. Por isso é uma boa prática que a condição do wait seja a primeira do bloco sincronizado. Ou que não tenha nada crítico antes dela.

Ele também faz uma observação que ele só libera o bloco sincronizado daquele monitor. Outros monitores que eventualmente estejam esperando se mantém esperando. É o caso de um objeto A com um método sincronizado, que chama o método sincronizado de outro objeto B, e esse sim chama o wait. O bloqueio de B é liberado, mas threads que tentarem atuar sobre A ainda receberão o bloqueio, pois A não liberou o seu monitor.

[quote=ViniGodoy]Sim, libera sim. Por isso é uma boa prática que a condição do wait seja a primeira do bloco sincronizado. Ou que não tenha nada crítico antes dela.

Ele também faz uma observação que ele só libera o bloco sincronizado daquele monitor. Outros monitores que eventualmente estejam esperando se mantém esperando. É o caso de um objeto A com um método sincronizado, que chama o método sincronizado de outro objeto B, e esse sim chama o wait. O bloqueio de B é liberado, mas threads que tentarem atuar sobre A ainda receberão o bloqueio, pois A não liberou o seu monitor.[/quote]

Hum, entendi Vini vlwww!!!