public class PingPong {
private final Object lock1;
private final Object lock2;
public PingPong {
lock1 = new Object();
lock2 = new Object();
super();
}
public void ping() {
synchronized (lock1) {
synchronized (lock2) { IO.println("ping"); }
}
}
public void pong() {
synchronized (lock2) {
synchronized (lock1) { IO.println("pong"); }
}
}
public static void main(String[] args) {
var pingpong = new PingPong();
Thread.ofPlatform().start(() -> {
for (;;) { pingpong.ping(); }
});
for (;;) { pingpong.pong(); }
}
}
Quel affichage obtient-on ?
Parfois des ping et des pong ... parfois que des ping ou que des pong ... ou rien !
Mais presque sûrement, ça bloque!
Le thread qui fait ping() prend le verrou du lock1,
et est dé-schédulé juste après (avant de prendre le verrou du lock2).
Le thread qui fait pong() prend le verrou du lock2,
et est dé-schédulé juste après (avant de prendre le verrou du lock1).
Le thread "ping" est re-schédulé et essaie de prendre le lock2... mais ne peut pas tant que le thread "pong" ne le lâche pas...
... et le thread "pong" ne le lâchera que s'il arrive à avoir le lock1. On est donc bloqués!
Si plusieurs threads (au moins 2) prennent plusieurs verrous (au moins 2), dans un ordre différent, ...
... il risque d'y avoir un problème d'interblocage.
Deux solutions :
volatile et des opérations atomiques spéciales), nous verrons cela en fin de semestre.
Si l'on connaît tous les locks du programme,
alors le programme ne peut pas atteindre un état d'interblocage.
Pourquoi ?
Pour simplifier
On suppose par l'absurde qu'il y a un interblocage et on va chercher une contradiction.
Le thread t1 attend le lock i qui est pris par t2.
Le thread t2 attend le lock j qui est pris par t1.
Comme lock i est déja pris par t2, on a lock i < lock j.
Comme lock j est déja pris par t1, on a lock j < lock i.
On aurait alors lock i < lock j ET lock j < lock i... on a une contradiction!
public class DeadLock {
private final Object lock1;
private final Object lock2;
private boolean finished;
public PingPong {
lock1 = new Object();
lock2 = new Object();
super();
}
public void process() throws InterruptedException {
synchronized (lock1) {
synchronized (lock2) {
while (!finished) {
lock2.wait();
}
}
}
IO.println("Finished !");
}
public void stop(){
synchronized (lock1) {
synchronized (lock2) {
finished = true;
lock2.notify();
}
}
}
public static void main(String[] args){
var deadLock = new DeadLock();
Thread.ofPlatform().start(()-> { deadLock.stop(); });
try{
deadLock.process();
catch (InterruptedException e) {
throw new AssertionError();
}
}
}
Attention, lock2.wait() rend le lock2 mais pas le lock1 !
public class A {
private final int value;
private static A a;
public A(int value) {
this.value = value
super();
}
public static void main(String[] args) {
Thread.ofPlatform().start(() -> {
if (a != null) {
IO.println(a.value);
}
// affiche 7 ou rien
});
a = new A(7);
}
}
Publication : un champ déclaré final est visible avec sa valeur d'initialisation par tous les threads après la fin du constructeur !
public class A {
private final int value;
private static A a;
public A(int value) {
this.value = value;
super();
}
public static void main(String[] args) {
Thread.ofPlatform().start(() -> {
if (a != null) {
IO.println(a.value);
}
// affiche 7 ou 0 !!! ou rien
});
a = new A(7);
}
}
Si on oublie le final, on peut voir la valeur par défaut.
Ici, c'est value = 0.
public class A {
private final int value;
private static A a;
A(int value) {
this.value = value;
A.a = this; // quelle drôle d'idée...
}
public static void main(String[] args) {
Thread.ofPlatform().start(() -> {
if (a != null) {
IO.println(a.value);
}
// affiche 7 ou 0 !!! ou rien
});
new A(7);
}
}
Si on publie this avant la fin du constructeur, il y a un risque aussi. Ça peut arriver, pour faire un singleton, mais là encore, ça n'est pas l'objet de ce cours...
Pour faire simple : ne le faites pas !
Pensez à mettre le super() à la fin du constructeur, ça évitera ce problème :)
Enfin, on pourrait avoir envie de démarrer un thread dans un constructeur.
Dans ce cas, le thread pourrait avoir accès à this (capturé dans l'environement du Runnable) avant qu'il soit publié...
start qui sera appelée après la construction de l'objet.
final ?synchronized dans le constructeur... Et on fait attention au super().
public class A {
private final int value;
private final Object lock;
public A(int value) {
var lock = new Object();
synchronized (lock) {
this.value = value;
}
this.lock = lock;
super();
}
public void increment(){
synchronized (lock) {
value++;
}
}
}
Il y a une autre façon de faire, en utilisant les champs volatile, mais on verra ça plus tard.
Les initialisations statiques (directes ou dans un bloc statique) sont visibles par tous les threads qui utilisent la classe.
class A {
private static Object o = new Object();
private static String[] strings = new String[100];
static {
Arrays.setAll(strings, __ -> "foo");
}
public static void main(String[] args) {
Thread.ofPlatform().start(() -> {
IO.println(A.o + " " + Arrays.toString(A.strings)); // pas de null
});
IO.println(A.o + " " + Arrays.toString(A.strings)); // pas de null
}
}
Donc pas de problème de publication avec les initialisations statiques.
Dans un constructeur,
thisDans un constructeur, pas besoin de synchronisation :
Dans les autres cas, il faut utiliser la synchronisation !