Bonjour,
J'aimerais avoir un petit conseil.
J'aurais voulu connaitre la meilleure manière de gérer le démarrage et l'arrêt d'un Thread
Ci-dessous, j'ai posté deux classes.
La première contient la méthode run du thread et j'aurais voulu savoir comment je pouvais arrêter et redemarrer ce même Thread à partir de la seconde classe.
merci d'avance à tous
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 public class FixTimeConsumer implements Runnable { ............ public void run() { while( true ) { try { List<TaskCS> taskCSs = oracleService.getResumeCSUs(); if ( !taskCSs .isEmpty() ) { new RunJobPlanThread( taskCSs ); } else { } Thread.sleep( 30000 ); } catch ( Exception e ) { throw new MainException( e ); } } } ............ }
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 public class RunJobPlanThread { ................ public void initRunJobPlanThread() { List<Callable<RunJobPlanTaskCallable>> tasks = new ArrayList<Callable<RunJobPlanTaskCallable>>(); //On ajoute ces taches à la liste Callable<RunJobPlanTaskCallable> runJobPlanTask; for ( TaskCS taskCS : taskCSs ) { runJobPlanTask = new RunJobPlanTask( taskCS ); tasks.add( runJobPlanTask ); } ExecutorService executor = Executors.newFixedThreadPool( fixedThreadPool ); launchRunJobPlanTask( executor, tasks ); } public void launchRunJobPlanTask( final ExecutorService executor, List<Callable<RunJobPlanTaskCallable>> tasks ) { CompletionService<RunJobPlanTaskCallable> completionService = new ExecutorCompletionService<RunJobPlanTaskCallable>( executor ); List<Future<RunJobPlanTaskCallable>> futures = new ArrayList<Future<RunJobPlanTaskCallable>>(); RunJobPlanTaskCallable runJobPlanTaskCallable = null; try { for( Callable<RunJobPlanTaskCallable> task : tasks ){ futures.add( completionService.submit( task ) ); } // Ici il faut arrêter le thread. for ( int i = 0; i < tasks.size(); i++ ) { runJobPlanTaskCallable = completionService.take().get(); if ( runJobPlanTaskCallable != null ) { String errorLevel = runJobPlanTaskCallable.getErrorLevel(); String errorMessage = runJobPlanTaskCallable.getErrorMessage(); this.runJobPlanTaskCallables.add( runJobPlanTaskCallable ); } if ( this.runJobPlanTaskCallables.size() == tasks.size() ) { } } } catch( Exception e ){ throw new MainException( e ); } finally { executor.shutdown(); } } ............... }
Partager