En una aplicación de swing, me gustaría volver a utilizar un hilo generado en lugar de crear uno nuevo para atender las solicitudes. Esto se debe a que las solicitudes llegarían en cortos intervalos de tiempo y el costo de crear un nuevo hilo para cada solicitud podría ser alto.Usar sleep() e interrupt() para reutilizar thread
estoy pensando en usar la interrupción() y sleep() métodos para hacer esto que a continuación y me gustaría saber los posibles problemas de rendimiento con el código:
public class MyUtils {
private static TabSwitcherThread tabSwitcherThread = null;
public static void handleStateChange(){
if(tabSwitcherThread == null || !tabSwitcherThread.isAlive()){
tabSwitcherThread = new TabSwitcherThread();
tabSwitcherThread.start();
}
else
tabSwitcherThread.interrupt();
}
private static class TabSwitcherThread extends Thread{
@Override
public void run() {
try {
//Serve request code
//Processing complete, sleep till next request is received (will be interrupted)
Thread.sleep(60000);
} catch (InterruptedException e) {
//Interrupted execute request
run();
}
//No request received till sleep completed so let the thread die
}
}
}
Gracias
En realidad, creo que eres TabSwitcherThread obtendrá excepción stackoverflow, tarde o temprano, porque tiene una llamada recursiva en su método run() :) – Alvin