Ceci est réalisé grâce au mot clef synchronized.
Dans un tel cas, il serait préférable d'éviter la contrainte de synchronisation à chaque fois en utilisant une clause synchronized.
In that case, you might as well avoid the overhead of synchronizing every time by using a synchronized clause.
Dans ce cas, la lecture ou la modification sont pratiquement toujours réalisées par des méthodes synchronized, ainsi vous pouvez examiner juste celle-là.
In this case, the reading or setting is virtually always accomplished via synchronized methods, so you can just examine those.
Avec la nouvelle instruction "synchronized", vous garantissez qu'une méthode n'est exécutée que par un seul robot à un instant donné.
You can use the new instruction "synchronized" in class definitions to allow execution of a method by only one bot at the same time.
Une approche possible est de créer une méthode synchronized qui appelle notify() pour son propre objet.
So one approach is to create a synchronized method that calls notify() for its own object.
Le wait(), comme décrit plus haut dans ce chapitre, doit être synchronized afin qu'il ait le verrou de l'objet.
The wait(), as described earlier in this chapter, must be synchronized so that it has the object lock.
Il évitait la question du multithreading en ignorant le mot clé synchronized (qui n'avait pas été encore introduit) et en rendant l'événement unicast.
That ducked out of the multithreading question by ignoring the synchronized keyword (which hadn't been introduced yet) and making the event unicast.
La section de code que vous voulez isoler de cette manière est appelé une section critique et vous utilisez le mot clé synchronized d'une manière différente pour créer une section critique.
The section of code you want to isolate this way is called a critical section and you use the synchronized keyword in a different way to set up a critical section.
Un troisième indice est de noter si la version de la classe de base de paint() est synchronized, ce qui n'est pas le cas.
A third clue is to notice whether the base-class version of paint() is synchronized, which it isn't.
Le résultat est que vous pouvez mettre un wait() dans n'importe quelle méthode synchronized, du fait qu'il y a du threading intervenant dans cette classe particulière.
As a result, you can put a wait() inside any synchronized method, regardless of whether there's any threading going on inside that particular class.
En fait, la seule place où vous pouvez appeler wait() ou notify() est dans une méthode ou un bloc synchronized.
In fact, the only place you can call wait() is within a synchronized method or block.
Quand vous voyez que ce n'est rien de trop grave, et un effet transitoire en plus, vous pouvez décider de laisser paint() un-synchronized pour éviter le temps supplémentaire de l'appel de méthode synchronized.
When you see that it's nothing too bad, and a transient effect at that, you can decide to leave paint() un-synchronized to prevent the extra overhead from the synchronized method call.
C'est l'approche la plus simple, puisque tout ce qu'elle requiert est d'ajouter le mot clé synchronized au finalizer et toutes les méthodes qui peuvent entrer en conflit avec (typiquement, toutes les méthodes).
It is the simplest approach, as all it requires is adding a synchronized keyword to the finalizer and all methods that can potentially conflict with it (typically all of them).