Synchronisation Thread

Résolu/Fermé
luchywa Messages postés 186 Date d'inscription mercredi 11 novembre 2009 Statut Membre Dernière intervention 6 avril 2017 - Modifié par KX le 30/05/2015 à 09:52
luchywa Messages postés 186 Date d'inscription mercredi 11 novembre 2009 Statut Membre Dernière intervention 6 avril 2017 - 30 mai 2015 à 22:32
Bonjour,

j'ai actuellement des difficultés à synchroniser mes Thread, j'ai fais un programme qui justement test l'efficacité des Thread, si vous lisez le programme vous comprendre-rez que normalement grâce à cette méthode "incr" synchronisé, je devrais avoir affiché sur ma console 3 trais successifs différents ensuite le mot "fini", et ce, deux fois (car 2 Thread), or elle m'affiche ceci dans un ordre complétement aléatoire qui prouve le non-fonctionnement de la synchronisation.

public class Test_Synchro extends Thread{
 
 public synchronized void incr(){ 
  int i;
  for(i=0;i<3;i++){ 
   System.out.println("-----");
   System.out.println("----------------------------------------------");try{Thread.sleep(1000);}catch(InterruptedException e){}}
   System.out.println("fini");   
  
 }
 public void run(){ 
   
  incr();
   
 }
 public static void main(String[] args){
  
  Test_Synchro thread = new Test_Synchro();
  thread.start();
  
  Test_Synchro thread2 = new Test_Synchro();
  thread2.start(); 
 }
}

Comprenez-vous d'où vient le problème?

Merci d'avance pour vos futures réponses.

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
30 mai 2015 à 10:07
Bonjour,

Le mot clé synchronized sur une méthode comme tu l'as fait mets un verrou sur l'objet courant. Or tu as deux objets distincts thread et thread2.
Quand incr() est verrouillée pour thread, elle ne l'est pas pour thread2 et inversement. Pour que ton code fonctionne, il faut qu'il y ait une synchronisation sur le même élément.

Le plus simple est de verrouiller la classe, en rajoutant le mot clé static à la méthode synchronized, l'élément commun sera alors la classe.
public static synchronized void incr() {

On peut également partager un objet commun qui fera l'action incr(), les deux threads se synchroniseront sur cet objet.

class ActionSynchro {

    public synchronized void incr() {
        for (int i = 0; i < 3; i++) {
            System.out.println("-----");
            System.out.println("----------------------------------------------");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) { }
        }
        System.out.println("fini");
    }
}

class ThreadSynchro extends Thread {

    private final ActionSynchro action;

    public ThreadSynchro(ActionSynchro action) {
        this.action = action;
    }

    @Override
    public void run() {
        action.incr();
    }
}

public class TestSynchro {

    public static void main(String[] args) {

        ActionSynchro commun = new ActionSynchro();

        ThreadSynchro thread1 = new ThreadSynchro(commun);
        thread1.start();

        ThreadSynchro thread2 = new ThreadSynchro(commun);
        thread2.start();
    }
}
1
luchywa Messages postés 186 Date d'inscription mercredi 11 novembre 2009 Statut Membre Dernière intervention 6 avril 2017 7
30 mai 2015 à 22:32
Merci à toi cela marche parfaitement et merci aussi pour tes explications.
0