...
This noncompliant code example uses a ReentrantLock
to protect a java.util.Date
instance, which is not thread-safe by design. The doSomethingSafely()
method must catch Throwable
to comply with guideline EXC06-J. Do not allow exceptions to transmit expose sensitive information.
Code Block | ||
---|---|---|
| ||
final class DateHandler { private final Date date = new Date(); final Lock lock = new ReentrantLock(); public void doSomethingSafely(String str) { try { doSomething(str); } catch(Throwable t) { // Forward to handler } } public void doSomething(String str) { lock.lock(); String dateString = date.toString(); if (str.equals(dateString)) { // ... } lock.unlock(); } } |
...