Fixed extraordinarily subtle race condition with contracts invariant

-- all of the methods in the class must be synchronized or the internal state can be inconsistent with the contract invariant when entering the class in a non-synchronized method, even when that method doesn't care about the object's internal state
This commit is contained in:
Mark DePristo 2011-10-17 13:37:55 -04:00
parent 2ebdff074c
commit fd4540cd32
1 changed files with 3 additions and 7 deletions

View File

@ -46,7 +46,7 @@ public class SimpleTimer {
* @return the name associated with this timer * @return the name associated with this timer
*/ */
@Ensures("result != null") @Ensures("result != null")
public String getName() { public synchronized String getName() {
return name; return name;
} }
@ -82,14 +82,14 @@ public class SimpleTimer {
/** /**
* @return is this timer running? * @return is this timer running?
*/ */
public boolean isRunning() { public synchronized boolean isRunning() {
return running; return running;
} }
/** /**
* @return A convenience function to obtain the current time in milliseconds from this timer * @return A convenience function to obtain the current time in milliseconds from this timer
*/ */
public long currentTime() { public synchronized long currentTime() {
return System.currentTimeMillis(); return System.currentTimeMillis();
} }
@ -119,8 +119,4 @@ public class SimpleTimer {
public synchronized double getElapsedTime() { public synchronized double getElapsedTime() {
return (running ? (currentTime() - startTime + elapsed) : elapsed) / 1000.0; return (running ? (currentTime() - startTime + elapsed) : elapsed) / 1000.0;
} }
public void printElapsedTime(PrintStream out) {
out.printf("SimpleTimer %s: %.2f%n", name, getElapsedTime());
}
} }