Exécution d'un thread dans un Executor
Bonjour
Voici mon problème. Ma première version d'un logiciel utilisait des threads exécutés de la manière la plus classique:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
@Override
protected void getProjectCompoundsAndResults(Project project)
throws IOException, SQLException
{
CompoundsGetter cg = new CompoundsGetter(project,
this.forCompoundLot);
Thread thread = new Thread(cg);
thread.start();
while (thread.isAlive()) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {}
}
} |
Notez d'ailleurs la variable "projet" qui est un objet contenant une List.
Puis, dans un souci d'être à la pointe, je me suis dis que c'était pas mal d'utiliser les Excecutor:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Override
protected void getProjectCompoundsAndResults(Project project)
throws IOException, SQLException
{
CompoundsGetter cg = new CompoundsGetter(project,
this.forCompoundLot);
//Thread thread = new Thread(cg);
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.submit(cg);
pool.shutdown();
/*thread.start();
while (thread.isAlive()) {
try {
Thread.sleep(500);
} catch (InterruptedException ex) {}
}*/
} |
Ca compile et ça s'exécute bien sauf qu'au debbugage, je peux voir qu'à l'initialisation, ma variable projet a bien sa List remplie mais dès que je suis en exécution (pool.submit(cg);), la liste apparaît vide !
Une précision, voici comment la méthode getProjectCompoundsAndResults est invoquée:
Code:
1 2 3 4 5 6 7 8 9 10 11
|
protected void writeProject(ResultSet rs)
throws SQLException, IOException
{
Logger.getLogger(ProjectsGetter.class.getName()).log(Level.INFO, "Creating project {0}", rs.getString(2));
Project project = new Project(rs.getInt(1), rs.getString(2),
rs.getString(3));
setProjectExperiments(project);
getProjectCompoundsAndResults(project);
project.getExperiments().clear();
} |
Merci d'avance de votre aide.
@++