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 :
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.
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); } }
J'ai donc créé une classe "XrdClThread" :
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
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 ==========================
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 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) { }
Et si j'utilise ma classe XrdClThread, je ne vois pas comment je vais pourvoir traiter cela...
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); }
Voilà dsl pour la masse de code, mais au moins je pense avoir été suffisamment clair pour obtenir de l'aide
Ciao
Partager