Thread parallèle

cs_amel2006 Messages postés 83 Date d'inscription samedi 21 janvier 2006 Statut Membre Dernière intervention 9 décembre 2006 - 8 mai 2006 à 20:26
super_toinou Messages postés 764 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 8 mars 2011 - 9 mai 2006 à 15:37
dans mon projet je doit se connecter à des moteur de recherche et récupérer des informations tel que dans mon programme je met ces information dans un fichier spécifie pour chaque source

pour la connexion je lance les thread à l'aide d'une boucle for ce qui me rend l'execution séquentielle et non pas parallèle

avez vous une solution à mon problème

mon code :
import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.regex.*;

public class connexion extends Thread {

private static String req = URLEncoder.encode("recherche");
// private static String langue = URLEncoder.encode("fr");//en ou any
// private static String format = URLEncoder.encode("any");
String nom;
String nomf;
public static Thread T1;


connexion (String s,String d ) {
nom=s;
nomf=d;
}

public void run()
{ try
{ URL url = new URL(nom);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
String userAgent = "agent utilisateur" ;
uc.setRequestProperty("User-Agent", userAgent);
InputStream in = uc.getInputStream();
int c =0;
StringBuffer build = new StringBuffer();
while (c != -1)
{
//lecture 1ere partie du caracter unicode
c = in.read()<<8;

//lecture de la 2eme partie
c=c | (in.read());
// c contient maintenant la valeur du caractere unicode
build.append((char) c);
}
System.out.println("fg;yhfghfgchuyghhhhhhhhhh");
File f = new File("temp/"+nomf+".txt");
DataOutputStream flux = new DataOutputStream(new FileOutputStream(f,true));
flux.writeChars(build.toString());
try {T1.sleep(1000); } catch (InterruptedException e) {System.out.println("sleep non marche"); }
flux.close();

}

catch (MalformedURLException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }

}
// xls
public static void main(String args[])
{ File rep = new File("temp");
if(!rep.exists()) rep.mkdirs();
String Langue = "en";
String Format = "html";// rtf
String [][] Tab_source=

{ {"Google","http://www.google.com/search?q="+req+"&meta=lr%3Dlang_"+Langue+"&as_filetype="+Format,"Gnrale","non cooperatif",""},
{"yahoo","http://fr.search.yahoo.com/search?fr=fp-tab-web-t-1&ei=ISO-8859-1&p="+req+"&meta=vl%3Dlang_"+Langue+"&vf="+Format,"Gnrale","non cooperatif",""},
{"Altavista","http://www.altavista.com/web/results'itag=ody&q="+req,"Gnrale","non cooperatif",""},
{"alltheweb","http://www.alltheweb.com/search?cat=web&cs=utf8&q="+req,"Gnrale","non cooperatif",""},
{"Doccimef","http://doccismef.chu-rouen.fr/servlets/Simple?Mot="+req,"Medecine","non cooperatif",""},
{"netster","http://www.netster.com/results/results.asp?Keywords="+req,"Gnrale","non cooperatif",""},
{"Droit","http://admi.net/cgi-bin/admijo.pl?requete=","Droit"+req,"non cooperatif",""},
{"Msn","http://search.msn.fr/results.aspx?qb=1&q="+req,"Gnrale","non cooperatif",""},
{"Electronique","http://www.abcelectronique.com/recherche/recherche.zphp?q="+req,"Electronique","non cooperatif",""},
{"info","http://www.x-recherche.com/cgi-bin/rech.pl?mots="+req,"Informatique","non cooperatif",""},
{"Lycos","http://vachercher.lycos.fr/cgi-bin/pursuit?query="+req,"Gnrale","non cooperatif",""},
{"commentcamarche","http://www.commentcamarche.net/search/search.php3?Mot="+req,"Informatique","non cooperatif",""},
{"dmoz","http://www.dmoz.ch/suche.cgi?q="+req,"Gnrale","non cooperatif",""} };
String [][] listselect1=new String[13][3];
int n=13;
for(int i = 0;i< n;i++)
{ for(int j = 0;j<3;j++) listselect1[i][j]= Tab_source[i][j];
T1 = new connexion( listselect1[i][1], listselect1[i][0]);
T1.start();
int time=0;
if (rep.isDirectory())
{
File[] list = rep.listFiles();
while((list.length!=i+1))
{ time+=1000;
try { T1.sleep(time); } catch (InterruptedException e) {System.out.println("sleep non marche"); }
list = rep.listFiles();
}
if(i==12)
{ time+=1000;
try {T1.sleep(time); } catch (InterruptedException e) {System.out.println("sleep non marche"); }
}
}
}

}

public static String remplacer(Matcher recherche, String remplacement)
{
return(recherche.replaceAll(remplacement));

}
public static String fichtostr(String nomf)
{
String tout = "" ;
try {

String ligne = "" ;
BufferedReader reader = new BufferedReader( new FileReader(nomf) );
while( (ligne = reader.readLine()) != null)
{
tout += ligne;
}
}

catch (IOException e) {
System.err.println(" Cela ne marche pas. Voila." + e.toString());
}
return tout;

}




}

merci de votre aide

3 réponses

super_toinou Messages postés 764 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 8 mars 2011 6
9 mai 2006 à 15:05
j ai po trop lu ton code mais c est koi ton pb en fait ??
0
cs_amel2006 Messages postés 83 Date d'inscription samedi 21 janvier 2006 Statut Membre Dernière intervention 9 décembre 2006 1
9 mai 2006 à 15:31
mon problème est que il n'ya pas de paralilisme entre les thread car je lance plusieurs thread à l'aide d'une boucle for mais à l'execution je vois la séquentiellité
merci de votre aide
0
super_toinou Messages postés 764 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 8 mars 2011 6
9 mai 2006 à 15:37
vu que j suis au taff j peux pas regarder en détail mais y a des trucs que j comprend pas !!
si ta thread T1 est en static ca veut dire que t utilise qu une seule thread a la fois (ce qui peut expliquer la séquentialité)
en + j vois pas pkoi t as un thread dans un thread (cad T1 est dans connnexion qui est déjà un thread)
pkoi tu fé pas une classe
Connexion extends Thread avec un constructeur qui prend comme info ou il doit se connecter et dans quel fichier il doit ecrire et dans une classe Programme tu lance tout !!!
pis a la fin de ta boucle tu fé un Thread.joinAll histoire d attendre que tout le monde ai fini et apres t affiche tout tes trucs !!
Courage !!
++ Toinou
0
Rejoignez-nous