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

Java Discussion :

Probleme de threads -> Arret instantanné


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre extrêmement actif
    Avatar de kedare
    Homme Profil pro
    SRE
    Inscrit en
    Juillet 2005
    Messages
    1 549
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Espagne

    Informations professionnelles :
    Activité : SRE

    Informations forums :
    Inscription : Juillet 2005
    Messages : 1 549
    Par défaut Probleme de threads -> Arret instantanné
    Salut a tous
    voila j'ai un probleme ,j'ai une application qui fait une utilisation intensive de threads, c'est un bot jabber , voila le code:

    Main.java
    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
     
    import connection.ConnectionThread;
     
    /**
     *
     * @author Kedare
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws Exception {
            String user = "kedare";
            String password = "***";
            String server = "jabber.fr";
            String channels[] = {"testing001@chat.jabberfr.org"};
            ConnectionThread chn = new ConnectionThread(server, user, password, channels);
            chn.start();
            chn.join();
     
        }
    }
    ConnectionThread.java
    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
    package connection;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    import channel.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.jivesoftware.smack.*;
     
    /**
     *
     * @author Kedare
     */
    public class ConnectionThread extends Thread {
     
        String user;
        String pass;
        String server;
        XMPPConnection connection;
        Logger log;
        String channels[];
     
        public ConnectionThread(String serverArg, String userArg, String passArg, String channelsArg[]) {
            this.user = userArg;
            this.pass = passArg;
            this.server = serverArg;
            this.channels = channelsArg;
            this.log = Logger.getLogger(ConnectionThread.class.getName());
            this.log.log(Level.INFO, "ConnectionThread created for " + this.server);
        }
     
        @Override
        public void run() {
            try {
                log.log(Level.INFO, "Connecting");
                this.connection = new XMPPConnection(this.server);
                this.connection.connect();
                this.connection.login(this.user, this.pass);
                this.log.log(Level.INFO, "Preparing to join channels");
                for (String channel : channels) {
                    this.log.log(Level.INFO, "Joining " + channel);
                    ChannelThread chan = new ChannelThread(channel,this.connection);
                    chan.start();
                    chan.join();
                }
            } catch (Exception ex) {
                this.log.log(Level.SEVERE, null, ex);
            }
        }
    }
    ChannelThread.java
    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
    package channel;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.jivesoftware.smack.*;
     
    /**
     *
     * @author Kedare
     */
    public class ChannelThread extends Thread {
     
        XMPPConnection connection;
        Logger log;
        String channelName;
        Chat channel;
     
     
        public ChannelThread(String channelArg, XMPPConnection connArg) {
            this.channelName = channelArg;
            this.connection = connArg;
            this.log = Logger.getLogger(ChannelThread.class.getName());
            this.log.log(Level.INFO, "ChannelnThread created for " + this.channelName);
        }
     
        @Override
        public void run() {
            try {
                this.log.log(Level.INFO, "Joining and begin messageListener for " + this.channelName);
                this.channel = this.connection.getChatManager().createChat(channelName, ChannelListeners.getMessageListener());
                this.channel.sendMessage("Hello");
                Thread.interrupted();
            } catch (Exception ex) {
                this.log.log(Level.SEVERE, null, ex);
            }
        }
    }
    ChannelListener.java
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package channel;
     
    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.MessageListener;
    import org.jivesoftware.smack.packet.Message;
     
    /**
     *
     * @author Kedare
     */
    public class ChannelListeners {
     
        static MessageListener messageListener = new MessageListener() {
     
            public void processMessage(Chat chat, Message message) {
                System.out.println("Receive Message : " + message);
            }
        };
     
        public static MessageListener getMessageListener() {
            return messageListener;
        }
    }
    en gros je voudrais que les threads ne s'arrete pas , et continuer a ecouter le listener , mais je n'y arrive pas :/
    savez vous comment faire ? c'est la premiere fois que j'utilise les threads comme ca..
    si non , la facon de coder est bonne ?
    merci

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 19
    Par défaut
    Suis pas expert mais à priori un thread est comme une méthode: quand il est fini il s'arrête!

    Pour avoir un thread permanent il faut qu'il contienne une boucle (do ou while etc.). Et pour qu'il laisse le temps aux autres threads de s'exécuter on inclue un
    qui fait pauser son exécution.

    Tu dois pouvoir trouver des exemples plus précis et riches ailleurs, cela-dit.

Discussions similaires

  1. Probleme fermeture Thread
    Par Raton dans le forum MFC
    Réponses: 4
    Dernier message: 29/09/2005, 09h51
  2. [Kylix] Problème de thread
    Par moltov dans le forum EDI
    Réponses: 1
    Dernier message: 22/06/2005, 13h28
  3. probleme breakpoint ne s arrete pas
    Par benoit70 dans le forum MFC
    Réponses: 4
    Dernier message: 16/03/2005, 11h24
  4. [C#] Probleme de points d'arret
    Par dekidec dans le forum Windows Forms
    Réponses: 4
    Dernier message: 14/02/2005, 23h08
  5. [Thread] comment arreter un thread qui execute une methode b
    Par Cyber@l dans le forum Concurrence et multi-thread
    Réponses: 8
    Dernier message: 04/08/2004, 10h51

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