Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/02/2014, 05:31
darkram
 
Fecha de Ingreso: septiembre-2007
Mensajes: 152
Antigüedad: 16 años, 7 meses
Puntos: 1
Thread dentro de bean Spring

Muy buenas,

Escribo a ver si alguien me puede ayudar con mi problema.

Primero les explico lo que estoy tratando de hacer, en el contexto spring tengo un bean el cual tiene un método init que inicializa un Thread y lo guarda en un variable static de la misma clase, este método init se llama solamente una vez cuando se ha terminado de arrancar el contexto, este mismo método init cuando se llama también hace un start del Thread, pinta un mensaje en la consola y luego lo paro con un wait hasta aquí todo bien.
Ahora lo que quiero hacer es desde cualquier parte del código conseguir ese bean que contiene el Thread previamente arrancado y darle un notify, pero me salta un error, adjunto el código y el error:

BEAN del contexto:
Código Java:
Ver original
  1. public class ConfirmacionesControllerBean implements ConfirmacionesController {
  2.     private static Thread confimacionesThread = null;
  3.     public void init(){
  4.             confimacionesThread = createThread();
  5.     }
  6.     public Thread createThread(){
  7.         Thread confirma = new Thread(){
  8.             public void run() {
  9.                 while(true){
  10.                     try {
  11.                         System.out.println("Tarea procesada");
  12.                         wait();
  13.                     } catch (InterruptedException e) {
  14.                         e.printStackTrace();
  15.                     }
  16.                 }
  17.             }
  18.         };
  19.         confirma.start();
  20.         return confirma;
  21.     }
  22.     public void wakeThread(){
  23.         confimacionesThread.notify();
  24.     }
  25. }

LLAMADAS A ESE BEAN:
getThreadConfirmaciones().init(); // esta OK, pinta la frase en la consola

getThreadConfirmaciones().wakeThread(); // esta es la que tiene ERROR

SALIDA CONSOLA:
Tarea procesada
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at com.ak.aitor.core.confirmaciones.ConfirmacionesCon trollerBean.wakeThread(ConfirmacionesControllerBea n.java:34)
at com.ak.aitor.sca.launch.LaunchServices.main(Launch Services.java:69)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at com.ak.aitor.core.confirmaciones.ConfirmacionesCon trollerBean$1.run(ConfirmacionesControllerBean.jav a:22)



Muchas gracias por vuestro tiempo,
Un saludo!