Synchronisations et communications entre Threads Synchronisation ...

5 downloads 9005 Views 253KB Size Report
APPLICATIONS DISTRIBUÉES. Par Laurent Magnin. Cours IFT 3880 & 6835, tous droits réservés / 2. Synchronisation des Threads. • Méthode yield(). • Méthode ...
Synchronisation des Threads • Méthode yield()

Synchronisations et communications entre Threads

• Méthode sleep() • Différences de priorité [EThread5]

IFT 3880-IFT 6835 APPLICATIONS DISTRIBUÉES Par Laurent Magnin

• Blocs synchronisés [EThread6*] • synchronized(objet){…} • Synchronized void myMethod{…} •

Synchronisation sur l’objet appelant la méthode



wait() - notify() & notifyAll() [EThread71]



join() [EThread72] Cours IFT 3880 & 6835, tous droits réservés / 2

Cycle de vie des Threads 1. 2. 3. 4. 5. 6. 7. 8.

Dangers liés à l’utilisation de Threads

thread makes wait() call thread tries to run in synchronized region but cannot acquire lock thread has been moved off the blocked queue by notify() or notifyAll(). thread has been woken up by some other thread either◦leaving the synchronized region◦making a wait call thread does an I/O call (including sleep) thread completes I/O call (including finishing sleep time) thread chosen to run thread yields CPU to some other ready thread either by • •

• Absence de synchronisations • Ordre des opérations incohérent • Problème d’accès aux ressources • Deadlocks (étreintes mortelles) • A attend B, lequel attend C, lequel attend A…

◦explicit yield() call ◦preempted by Java kernel

Cours IFT 3880 & 6835, tous droits réservés / 3

Communication entre Threads

Cours IFT 3880 & 6835, tous droits réservés / 4

Lecture de Streams / Char

• Variables synchronisées • Exemple Producteur / Consommateur [EThread9*] • Par files de messages (Stream) • Local (EThread8) • Sockets (prochain cours)

Cours IFT 3880 & 6835, tous droits réservés / 5

Cours IFT 3880 & 6835, tous droits réservés / 6

1

Écriture de Streams / Char

Lecture de Streams / Byte

Cours IFT 3880 & 6835, tous droits réservés / 7

Écriture de Streams / Byte

Cours IFT 3880 & 6835, tous droits réservés / 8

Utilisation cumulée de filtres de Stream

FilterInputStream(InputStream)

Cours IFT 3880 & 6835, tous droits réservés / 9

Cours IFT 3880 & 6835, tous droits réservés / 10

The Concurrency Utilities • Package java.util.concurrent • Introduit avec Java 1.5 • Utilitaires basés sur les threads • Exemples (03_Java5_.zip) tirés du livre “Java : the

complete reference, J2SE 5th edition” par Schildt, herbert (disponible sous forme électronique à la bibliothèque)

Cours IFT 3880 & 6835, tous droits réservés / 11

2

Suggest Documents