----- Original Message -----Sent: Wednesday, February 28, 2007 4:30 PMSubject: [Java] problemas con ThreadPool
Hola a todos,
Tengo el siguiente inconveniente: tengo una clase para manejar los Thread por grupos que se crean con ThreadGroup. Adicionalmente una clase para asignar estas tareas. El problema radica en que se instancia el ThreadPool, se adicionan los objetos implementados de Runnable o extendidos de Thread (ejemplo un sleep(60000) ) y termina repentinamente antes de completar la tarea.
Adjunto Codigo
public class ThreadPool {
private final String _nombre;
private ThreadGroup _grupo;
private List _trabajos = new ArrayList();
public ThreadPool(String name, boolean daemon) {
this(new ThreadGroup(
name), daemon); }
public ThreadPool(ThreadGr
oup group, boolean daemon) { _nombre = group.getName(
); _grupo = new ThreadGroup(
_nombre);
if (daemon) {
_grupo.setDaemon(
true); }
}
public void addTarea(Runnable tarea){
String nombre = _nombre+"-tarea-"+_trabajos.size(
); _trabajos.add(
new ThreadPoolWorker( _grupo,tarea, nombre)); }
}
public class ThreadPoolWorker {
private ThreadGroup _group;
private Thread _worker;
private static final Log log = LogFactory.getLog(
ThreadPoolWorker .class);
public ThreadPoolWorker(
ThreadGroup group, Runnable tarea, String name) { _group = group;
_worker = new Thread(_group, tarea, name);
_worker.setDaemon(
_group.isDaemon( )); log.debug("Thread " + _worker.getName(
) ); }
}
Clases de Test
public class ThreadPoolManagerTe
st extends TestCase {
public ThreadPoolManagerTe
st(String testName) { super(testName)
; }
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
}
/**
* Test of crearThreadPool method, of class com.selsa.linnet.
commons.thread. ThreadPoolManage r. */
public void testCrearThreadPool
() throws Exception { System.out.println("crearThreadPool");
String nombre = "Administrar";
ThreadPoolManager instance = new ThreadPoolManager(
);
ThreadPool result = instance.crearThrea
dPool(nombre) ;
result.addTarea( new ThreadTest() );
// TODO review the generated test code and remove the default call to fail.
}
}
public class ThreadTest extends Thread implements Serializable{
/** Creates a new instance of testThread */
public ThreadTest() {
}
public void run(){
while(true){
System.out.println("tarea");
try {
System.out.println("Esperando 1 minuto");
sleep(60000)
; System.out.println("Termina Espera");
} catch (InterruptedExcepti
on ex) { ex.printStackTrace(
); }
}
}
}
Para este caso, termina antes de completar el Minuto. Exactamente cuando termina de leer el codigo de las clases.
Alguna idea
Cordialmente,
Juan Carlos Burgos Pulido