IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Concurrence et multi-thread Java Discussion :

utilisation du Multi threading


Sujet :

Concurrence et multi-thread Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2005
    Messages
    288
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 288
    Par défaut utilisation du Multi threading
    Bien l'bonjour tout le monde!

    Dans le cadre du développement d'une application devant fonctionner sous Linux je souhaite utiliser le multi threading afin de pouvoir lancer des commandes simultanément... mais j'ai un peu de mal

    Cas de figure : j'ai une interface dans laquelle l'utilisateur renseigne des champs correspondant à des chemins (jusk là tout va bien ). Lorsqu'il clik sur le bouton "Next" je lance une fonction "backgroundCommand()" : c'est par le biais de celle-ci ke je réalise mes commandes.
    Jusqu'à présent, et afin de tester, je n'ai pas utilisé de thread :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    public boolean backgroundCommand(String serverName)
     {
      try
      {
       // to know if serverName is current server
       boolean isServerName = false;
       con = new XrdClConnectionDataBase();
       String s = null;
       int rc = 0;
       //Process proc;
       Runtime rt;
       // recuperation of the attributes of the project (saved during step 1 in the database)
       sql = "SELECT * FROM project where projectName = \'" + projectName + "\'";
       st  = con.getConnection().createStatement();
       rs  = st.executeQuery(sql);
       rs.next();
       // server name on which the application is running
       String command = "hostname";
       rt = Runtime.getRuntime();
       /*XrdClThread thread1 = new XrdClThread(rt, command);
       thread1.start();*/
     
       Process proc = rt.exec(command);
       int rc = proc.waitFor(); // l'application attend que ce processus soit fini
       BufferedReader stdout = new BufferedReader(new InputStreamReader(thread1.getProcess().getInputStream()));
       BufferedReader stderr = new BufferedReader(new InputStreamReader(thread1.getProcess().getErrorStream()));
       if((s = stdout.readLine()) != null)
       {
         if(s.equals(serverName))
          isServerName = true;
         System.out.println(isServerName);
       }
       while ((s = stderr.readLine()) != null)
       {
        System.out.println("Error :\n");
        System.out.println(s);
       }
       s = null;
       getBeginningDirectory(rs.getString("XRDBINPATH"));
       getLastDirectory(rs.getString("XRDBINPATH"));
       if(isServerName)
       {
        if(!findDirectory(getBeginningDirectory(rs.getString("XRDBINPATH")),getLastDirectory(rs.getString("XRDBINPATH"))))
        {
         // creation of xrd bin directory on the server
         command = "mkdir -p " + getBeginningDirectory(rs.getString("XRDBINPATH")) + getLastDirectory(rs.getString("XRDBINPATH"));
         System.out.println(command);
         rt = Runtime.getRuntime();
         proc = rt.exec(command);
         rc = proc.waitFor();
         stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
         while ((s = stderr.readLine()) != null)
         {
          System.out.println("Error :\n");
          System.out.println(s);
         }
         System.out.println("bin directory created in " + getBeginningDirectory(rs.getString("XRDBINPATH")) + " for " + serverName);
        }
        if(!findDirectory(getBeginningDirectory(rs.getString("OLBBINPATH")),getLastDirectory(rs.getString("OLBBINPATH"))))
        {
         s = null;
         // creation of load balancer bin directory on the server
         command = "mkdir -p " + getBeginningDirectory(rs.getString("OLBBINPATH")) + getLastDirectory(rs.getString("OLBBINPATH"));
         System.out.println(command);
         rt = Runtime.getRuntime();
         proc = rt.exec(command);
         rc = proc.waitFor();
         stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
         while ((s = stderr.readLine()) != null)
         {
          System.out.println("Error :\n");
          System.out.println(s);
         }
         System.out.println("bin directory created in " + getBeginningDirectory(rs.getString("OLBBINPATH")) + " for " + serverName);
        }
     
    ...............
     
    for(int l = 0; l < tabXrdLibFiles.size(); l++)
        {
         s = null;
         // move bin from detared file to path chose by the user
         command = rs.getString("RCPPATH") + " -p " + xrootdDir + "/" + projectName + "/" + tarBallDir + "/lib/" + tabXrdLibFiles.get(l).toString() + " " + rs.getString("XRDLIBPATH");
         System.out.println(command);
         rt = Runtime.getRuntime();
         proc = rt.exec(command);
         rc = proc.waitFor(); // l'application attend que ce processus soit fini
         stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
         while ((s = stderr.readLine()) != null)
         {
          System.out.println("Error :\n");
          System.out.println(s);
         }
        }
       }
       return true;
      }
          catch(Exception e)
          {
          e.printStackTrace();
          return false;
         }
      finally
      {
       try
       {
        if (con != null)
         con.fermeture();
       }
       catch (Exception excptio)
       {
        System.err.println(excptio);
       }
      }
    ce n'est qu'un petit bout de mon code et comme vous pouvez vous en rendre compte c'est trés tés lourd.

    J'ai donc créé une classe "XrdClThread" :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    // =====  Définition of the class: XrdClThread  ======================
    public class XrdClThread extends Thread
    {
     //-----  the attributes  ----------------------------------------------------
        /** the attributes of a server */
        private Runtime runtime;
        private String command;
     /** Constructor */
     public XrdClThread(Runtime rt, String str)
     {
        runtime = rt;
        command = str;
     }
     /** Function */
     public void run()
     {
      try
      {
       proc = runtime.exec(command);
       int rc = proc.waitFor();
      }
      catch(Exception e)
      {
       e.printStackTrace();
      }
     }
     //-----  the get functions  ---------------------------------------------------
     /** get the runtime
      @return Runtime */
        public Runtime getRuntime() { return runtime; }
        /** get the runtime
      @return Runtime */
        public String getCommand() { return command; }
        //-----  the set functions  ------------------------------------------------
     /** modify the runtime
      @param Runtime rt
      @return  void */
        public void setRuntime(Runtime rt) { runtime = rt; }
        /** modify the command
      @param String cd
      @return  void */
        public void setCommand(String cd) { command = cd; }
    }
    // =========================== End of the XrdClThread class  ==========================
    et après dans ma fonction backgroundCommand() je comptais faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Runtime rt = Runtime.getRuntime();
          XrdClThread thread1 = new XrdClThread (rt, cmd1);
          XrdClThread thread2 = new XrdClThread (rt, cmd2);
          thread1.start();
          thread2.start();
          try {
          thread1.join(5);
          thread2.join(5);
          } catch(Exception e) { }
    Cependant vous aurez surement remarqué que dans mon code de départ, je récupère la sortie de mes commandes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.().getInputStream()));
       BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
       if((s = stdout.readLine()) != null)
       {
         if(s.equals(serverName))
          isServerName = true;
         System.out.println(isServerName);
       }
       while ((s = stderr.readLine()) != null)
       {
        System.out.println("Error :\n");
        System.out.println(s);
       }
    Et si j'utilise ma classe XrdClThread, je ne vois pas comment je vais pourvoir traiter cela...

    Voilà dsl pour la masse de code, mais au moins je pense avoir été suffisamment clair pour obtenir de l'aide

    Ciao

  2. #2
    Expert confirmé
    Avatar de Jedai
    Homme Profil pro
    Enseignant
    Inscrit en
    Avril 2003
    Messages
    6 245
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Avril 2003
    Messages : 6 245
    Par défaut
    Eh bien tu mets les sorties dans des variables de tes threads et tu examines ces sorties après les join(), non ?

    --
    Jedaï

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2005
    Messages
    288
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 288
    Par défaut
    c'est bien beau mais comment j'y accède aux sorties si j'utilise une autre classe pour lancer le thread ?

  4. #4
    Membre Expert
    Avatar de xavlours
    Inscrit en
    Février 2004
    Messages
    1 832
    Détails du profil
    Informations forums :
    Inscription : Février 2004
    Messages : 1 832
    Par défaut
    Je n'ai pas lu le code en détail, mais pour répondre à ta question, la classe qui lance le thread appelle join() pour attendre sa fin, puis appelle des getters pour récupérer la valeur des variables.
    "Le bon ni le mauvais ne me feraient de peine si si si je savais que j'en aurais l'étrenne." B.V.
    Non au langage SMS ! Je ne répondrai pas aux questions techniques par MP.
    Eclipse : News, FAQ, Cours, Livres, Blogs.Et moi.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Utilisation du multi-threading dans les jeux vidéos
    Par Dalini71 dans le forum Développement 2D, 3D et Jeux
    Réponses: 11
    Dernier message: 10/10/2011, 12h45
  2. Utilisation d'un EntityManager et multi-threading
    Par ::Fistons dans le forum Tomcat et TomEE
    Réponses: 0
    Dernier message: 26/01/2011, 11h36
  3. Réponses: 27
    Dernier message: 30/09/2010, 21h46
  4. Graphique Utilisation CPU Multi Thread
    Par OlivierDotre dans le forum C
    Réponses: 0
    Dernier message: 19/08/2010, 14h18
  5. L'utilisation de synchronized dans une application multi-thread
    Par Tigrounette dans le forum Général Java
    Réponses: 9
    Dernier message: 08/04/2008, 11h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo