- 1. tryLock(): coger el cerrojo si es posible, sino devuelve false.
- 2. tryLock(long timeout, TimeUnit unit): coger el cerrojo antes del timeout y devuelve false después del timeout.
- 3. lockInterruptibly():intenta coger el cerrojo y entrar en el estado wait, pero puede ser interrumpido por otro hilos.
Para iniciarlo:
private Lock cerrojo = new ReentrantLock();
Un ejemplo de como asegurar una variable:
dialog.addWindowListener(new WindowAdapter(){ publicvoid windowClosing(WindowEvent we){ cerrojo.lock(); desconectando = true; cerrojo.unlock(); } });
Aquí tenéis la documentación: Enlace.
No hay comentarios:
Publicar un comentario