...
Code Block | ||
---|---|---|
| ||
public final class Client {
public void doSomething(File file) {
final Lock lock = new ReentrantLock();
InputStream in = null;
try {
lock.lock();
in = new FileInputStream(file);
// Perform operations on the open file
lock.unlock();
} catch (FileNotFoundException x) {
// Handle exception
} finally {
if (in != null) {
try {
in.close();
} catch (IOException x) {
// Handle exception
}
}
}
}
}
|
...
Code Block | ||
---|---|---|
| ||
public final class Client {
public void doSomething(File file) {
final Lock lock = new ReentrantLock();
InputStream in = null;
lock.lock();
try {
in = new FileInputStream(file);
// Perform operations on the open file
} catch (FileNotFoundException fnf) {
// Forward to handler
} finally {
lock.unlock();
if (in != null) {
try {
in.close();
} catch (IOException e) {
// Forward to handler
}
}
}
}
}
|
...
Code Block | ||
---|---|---|
| ||
final class DateHandler {
private final Date date = new Date();
final Lock lock = new ReentrantLock();
public void doSomething(String str) {
lock.lock();
try {
String dateString = date.toString();
if (str != null && str.equals(dateString)) {
// ...
}
// ...
} finally {
lock.unlock();
}
}
}
|
...