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 :

[Thread] Besoin d'aide .


Sujet :

Concurrence et multi-thread Java

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 3
    Points : 3
    Points
    3
    Par défaut [Thread] Besoin d'aide .
    bonjour ,
    J'ai besoin d'un peu d'aide sur les Threads .
    J'ai écris une classe permettant de faire mes recherches dans ma base SQL et je voudrais que la méthode 'executerLaRecherche()' soit executée dans un thread de manière a rendre la main avant que la requete soit terminée (car elle peut prendre beaucoup de temps : 3 à 4 minutes).
    J'ai donc voulu créer un Thread , le probleme c'est que dans la methode run de mon Thread , les différents attributs de ma classe ne sont pas accessible .
    Quelqu'un pourrait m'aider et me dire comment je pourrais faire ?
    Merci d'avance de votre aide .

    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
    package net.dev;
    import java.sql.*;
    import net.dev.connecteurSQL;;
     
    public class rechercheSQL {
     
    	private String laRequeteSQL;
    	private Connection laConnexion;
    	public ResultSet leResultat;
     
    	public rechercheSQL () throws SQLException
    	{
    		this.laConnexion = null;
    		this.laRequeteSQL = null;
    		this.leResultat = null;
    	}
     
    	public void executerLaRecherche () throws SQLException
    	{
    		Thread monThread = new Thread ()
    		{
    			public void run ()
    			{
    				if (this.laConnexion != null && this.laRequeteSQL != null)
    				{
    					Statement instruction = this.laConnexion.createStatement();
    					this.leResultat = instruction.executeQuery(this.laRequeteSQL);
    				}
    			}
    		};
    		monThread.start();
    	}
     
    	public void setLaRequeteSQL (String req) throws SQLException
    	{
    		this.laRequeteSQL = req;
    	}
     
    	public void setlaConnexion (connecteurSQL monConnecteur) throws SQLException
    	{
    		this.laConnexion = monConnecteur.getConnexion();
    		this.leResultat = null;
    	}
    }
    Pour info , sans le thread , ce code-ci fonctionne tres bien :

    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
    package net.dev;
    import java.sql.*;
    import net.dev.connecteurSQL;;
     
    public class rechercheSQL {
     
    	private String laRequeteSQL;
    	private Connection laConnexion;
    	public ResultSet leResultat;
     
    	public rechercheSQL () throws SQLException
    	{
    		this.laConnexion = null;
    		this.laRequeteSQL = null;
    		this.leResultat = null;
    	}
     
    	public void executerLaRecherche () throws SQLException
    	{
    		if (this.laConnexion != null && this.laRequeteSQL != null)
    			{
    				Statement instruction = this.laConnexion.createStatement();
    				this.leResultat = instruction.executeQuery(this.laRequeteSQL);
    			}
    	}
     
    	public void setLaRequeteSQL (String req) throws SQLException
    	{
    		this.laRequeteSQL = req;
    	}
     
    	public void setlaConnexion (connecteurSQL monConnecteur) throws SQLException
    	{
    		this.laConnexion = monConnecteur.getConnexion();
    		this.leResultat = null;
    	}
    }

  2. #2
    Candidat au Club
    Profil pro
    Inscrit en
    Août 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2009
    Messages : 3
    Points : 3
    Points
    3
    Par défaut
    J'ai trouvé :

    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
    	public void executerLaRecherche () throws SQLException
    	{
    		Thread monThread = new Thread ()
    		{
    			public void run ()
    			{
    				try
    				{
    					executerLaRequete();
    				}
    				catch (Exception ex)
    				{
     
    				}
    			}
    		};
    		monThread.start();
    	}
     
    	public void executerLaRequete() throws SQLException
    	{
    		if (this.laConnexion != null && this.laRequeteSQL != null)
    		{
    			Statement instruction = this.laConnexion.createStatement();
    			this.leResultat = instruction.executeQuery(this.laRequeteSQL);
    		}
    	}
    Si ca peut aider ....

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

Discussions similaires

  1. 1ere utilisation des threads : besoin d'aide.
    Par sp2308 dans le forum Débuter
    Réponses: 4
    Dernier message: 27/01/2014, 22h47
  2. Réponses: 4
    Dernier message: 23/12/2008, 08h34
  3. Thread besoin d'aide
    Par storm_2000 dans le forum Débuter avec Java
    Réponses: 0
    Dernier message: 25/09/2008, 18h03
  4. Besoin d'aide concernant l'utilisation de thread
    Par romain69 dans le forum Windows Forms
    Réponses: 1
    Dernier message: 06/03/2008, 17h21
  5. Thread et synchronized. besoin d'aide.
    Par storm_2000 dans le forum Débuter avec Java
    Réponses: 6
    Dernier message: 30/01/2007, 20h58

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