Threads... 2ª parte

Salve, Salve Galera!

Alguns conceitos…

wait() - Coloca a thread atual para esperar até que seja notificada. Lança a exception InterruptedException!!!

notify() - Notifica UMA apenas UMA thread para que o escalonador selecione e execute…

notifyAll() - Notifica TODAS as threads, elas ficam “brigando entre si” até que o escalonador escolhe quem vai ser executada…

Correto?? Alguém quer adicionar algo? :slight_smile:

Atn.
Dennys Fredericci
http://www.dennys.eti.br

No wait() se ela não for notificada e nao tiver um tempo pra ficar em espera, ela para ali.
8O

Apenas complementando:

notifyAll() notifica todas as threads que estejam bloqueadas para o objeto que estiver fazendo a notificação.

Procedimento semalhante ocorre para notify() :lol:

[quote=“Robson”]Apenas complementando:

notifyAll() notifica todas as threads que estejam bloqueadas para o objeto que estiver fazendo a notificação.

Procedimento semalhante ocorre para notify() :lol:[/quote]

Hum… é mesmo!? Pensei que fosse TODAS as threads sem exceção…

Atn.
Dennys Fredericci
http://www.dennys.eti.br

Dennys,

Aí vão dois trechos do livro da Kathy Sierra que ilustram melhor:

[quote]
The methods wait() and notify(), remember, are instance methods of
Object. In the same way that every object has a lock, every object can have a list of
threads that are waiting for a signal (a notification) from the object. A thread gets on
this waiting list by executing the wait() method of the target object. From that
moment, it doesn?t execute any further instructions until the notify() method of
the target object is called. If many threads are waiting on the same object, only one
will be chosen (in no guaranteed order) to proceed with its execution. If there are no
threads waiting, then no particular action is taken.

As we said earlier, an object can have many threads waiting on it, and using
notify() will affect only one of them. Which one exactly is not specified and
depends on the JVM implementation, so you should never rely on a particular thread
being notified in preference to another.
In cases in which there might be a lot more waiting, the best way to do this is by
using notifyAll().
[/quote] :wink:

[quote=“Robson”]Apenas complementando:

notifyAll() notifica todas as threads que estejam bloqueadas para o objeto que estiver fazendo a notificação.

Procedimento semalhante ocorre para notify() :lol:[/quote]

Hum… é isso mesmo… fiz uns testes aqui… valeu pela dica! :slight_smile:

Atn.
Dennys Fredericci
http://www.dennys.eti.br