Estoy usando el CountDownLatch para sincronizar un proceso de inicialización entre dos hilos y me preguntaba sobre el manejo correcto del InterruptedException que podría arrojar.CountDownLatch InterruptedException
el código que escribió inicialmente era la siguiente:
private CountDownLatch initWaitHandle = new CountDownLatch(1);
/**
* This method will block until the thread has fully initialized, this should only be called from different threads Ensure that the thread has started before this is called.
*/
public void ensureInitialized()
{
assert this.isAlive() : "The thread should be started before calling this method.";
assert Thread.currentThread() != this, "This should be called from a different thread (potential deadlock)";
while(true)
{
try
{
//we wait until the updater thread initializes the cache
//that way we know
initWaitHandle.await();
break;//if we get here the latch is zero and we are done
}
catch (InterruptedException e)
{
LOG.warn("Thread interrupted", e);
}
}
}
¿Este patrón tiene sentido? Básicamente es una buena idea ignorar el InterruptedException, solo sigue esperando hasta que tenga éxito. Supongo que simplemente no entiendo las situaciones en las que esto sería interrumpido, así que no sé si debería manejarlas de manera diferente.
¿Por qué se arrojaría una InterruptedException aquí, cuál es la mejor práctica para manejarla?
Gah Traté de corregir el formato y se produjo un conflicto de edición. No tengo opción de retroceder. ¡Lo siento! –
Ya lo enrollé. ¡Gracias por la ayuda! Me di cuenta de que estaba mal, pero decidí corregirlo al mismo tiempo que escribía el muro de texto después. – jdmichal
En realidad, no, estoy realmente acostumbrado a Wikipedia, donde hace clic en deshacer en la edición que desea deshacer, no en la revisión que desea deshacer ** a **. –