Como bloquear com ReentrantLock?
Eu esperaria que o teste a seguir imprima apenas "foi bloqueado" uma vez. MAS, consequentemente, imprime a linha.
public class LocKTest {
@Test
public void testLock() {
Lock lock = new ReentrantLock();
while (true) {
if (lock.tryLock()) {
//lock.lock(); same result if I include an explicit lock here
System.out.println("has been locked");
}
}
}
}
Tanto quanto eu entendi,tryLock
irá bloquear oReentrantLock
se possível (ou seja, se ainda não estiver bloqueado). Mas, obviamente, este não é o caso.
Como posso definir um bloqueio de thread seguro?