...
Code Block |
---|
|
final class ControlledStop implements Runnable {
private boolean done = false;
public void run() {
while (!done) {
try {
// ...
Thread.currentThread().sleep(1000); // Do something
} catch(InterruptedException ie) {
// handle exception
}
}
}
protected void shutdown(){
done = true;
}
}
|
...
Code Block |
---|
|
class BankOperation {
private int balance = 0;
private boolean initialized = false;
public BankOperation() {
if (!performAccountVerification()) {
throw new SecurityException("Invalid Account");
}
balance = 1000;
initialized = true;
}
private int getBalance() {
if (initialized == true) {
return balance;
}
else {
return -1;
}
}
}
|
Compliant Solution
This compliant solution declares the initialized
flag as volatile
to ensure that the initialization statements are not reordered.
...
Code Block |
---|
|
class Holder {
volatile ImmutablePoint ipoint;
Holder(ImmutablePoint ip) {
ipoint = ip;
}
void getPoint() {
return ipoint();
}
void setPoint(ImmutablePoint ip) {
this.ipoint = ip;
}
}
|
...