KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > zirc > threads > ThreadProcessor


1 package zirc.threads ;
2
3 import java.util.* ;
4
5 //zIrc, irc client.
6
// Copyright (C) 2004 CoolBytes(Stephane claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com
7
//
8
// This program is free software; you can redistribute it and/or
9
// modify it under the terms of the GNU General Public License
10
// as published by the Free Software Foundation; either version 2
11
// of the License, or (at your option) any later version.
12
//
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
// GNU General Public License for more details.
17

18 /**
19  * <p>Title: ThreadProcessor</p>
20  * <p>Description: thread qui gere une file de thread, sans priorites</p>
21  * <p>Copyright: Copyright (c) 2004</p>
22  * <p>Company: CoolBytes(Stephane Claret, Andre Aymon, Alban Zumofen) coolbytes@hotmail.com</p>
23  * @version 1.0
24  */

25
26 public class ThreadProcessor extends Thread JavaDoc
27 {
28   //informe si le thread tourne ou pas
29
private boolean isRunning;
30
31   //passer arret a vrai pour interrompre les taches en cours
32
private boolean arret ;
33
34   //arraylist de tache a effectuer
35
private LinkedList threadArray = new LinkedList() ;
36
37   public ThreadProcessor()
38   {
39   }
40
41   /**
42    * run
43    * va tourner en boucle tant que le boolean run est sur true. Si on a aucun
44    * pseudothread a traiter dans l'array, le thread se termine. Si on a des thread dans l'array,
45    * on les traite, un apres
46    * l'autre. dans l'ordre d'ajout
47    */

48   public void run()
49   {
50     //met le flag d'etat a true
51
isRunning = true;
52
53     //thread a executer
54
PseudoThread tache;
55
56     while (!arret && (threadArray.size() > 0 ) )
57     {
58        tache = (PseudoThread)(threadArray.get(0));
59
60        //lancer la tache
61
if(tache != null)
62        {
63          tache.go();
64        }
65
66        //la retirer de l'arraylist
67
threadArray.remove(tache);
68     }
69
70     isRunning = false;
71   }
72
73   /**
74    * addThread sert a jouter un pseudothread dans la liste d'attente
75    *
76    * @param _thread Thread
77    */

78   public void addThread(PseudoThread _thread)
79   {
80     //ajouter le thread
81
threadArray.add(_thread) ;
82
83     //si inactif, redémarrer
84
if(isRunning == false)
85     {
86       run();
87     }
88   }
89
90   /**
91    * runThread
92    * sert a arreter le thread quand c'est necessaire
93    *
94    * @param _run boolean
95    */

96   public void arrete()
97   {
98      arret = true;
99   }
100
101 }
102
Popular Tags