Skip to content

Commit

Permalink
PLANNER-1064 Solver.getTimeMillisSpent() and Solver.getBestScore() ne…
Browse files Browse the repository at this point in the history
…ed to be thread safe
  • Loading branch information
ge0ffrey committed Mar 20, 2018
1 parent f19a096 commit 752c363
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Expand Up @@ -52,7 +52,7 @@ public interface Solver<Solution_> {
* {@link SolverEventListener#bestSolutionChanged(BestSolutionChangedEvent)} is often more appropriate).
* <p>
* This method is thread-safe.
* @return never null, but it can return the original, uninitialized {@link PlanningSolution} with a {@link Score} null.
* @return never null, but it can return the uninitialized {@link PlanningSolution} with a {@link Score} null.
*/
Solution_ getBestSolution();

Expand All @@ -61,12 +61,22 @@ public interface Solver<Solution_> {
* <p>
* This is useful for generic code, which doesn't know the type of the {@link PlanningSolution}
* to retrieve the {@link Score} from the {@link #getBestSolution()} easily.
* <p>
* This method is thread-safe.
* @return null if the {@link PlanningSolution} is still uninitialized
*/
Score getBestScore();

/**
* @return the amount of millis spent between when this solver started (or last restarted) and ended
* Returns the amount of milliseconds spent solving since the last start.
* If it hasn't started it yet, it runs 0.
* If it hasn't ended yet, it returns the time between the last start and now.
* If it has ended already, it returns the time between the last start and the ending.
* <p>
* A {@link #addProblemFactChange(ProblemFactChange)} triggers a restart which resets this time.
* <p>
* This method is thread-safe.
* @return the amount of milliseconds spent solving since the last (re)start, at least 0
*/
long getTimeMillisSpent();

Expand Down
Expand Up @@ -109,11 +109,17 @@ public Score getBestScore() {

@Override
public long getTimeMillisSpent() {
Long startingSystemTimeMillis = solverScope.getStartingSystemTimeMillis();
if (startingSystemTimeMillis == null) {
// The solver hasn't started yet
return 0L;
}
Long endingSystemTimeMillis = solverScope.getEndingSystemTimeMillis();
if (endingSystemTimeMillis == null) {
// The solver hasn't ended yet
endingSystemTimeMillis = System.currentTimeMillis();
}
return endingSystemTimeMillis - solverScope.getStartingSystemTimeMillis();
return endingSystemTimeMillis - startingSystemTimeMillis;
}

@Override
Expand Down
Expand Up @@ -47,8 +47,8 @@ public class DefaultSolverScope<Solution_> {
*/
protected Semaphore runnableThreadSemaphore = null;

protected Long startingSystemTimeMillis;
protected Long endingSystemTimeMillis;
protected volatile Long startingSystemTimeMillis;
protected volatile Long endingSystemTimeMillis;

protected Score startingInitializedScore;

Expand Down

0 comments on commit 752c363

Please sign in to comment.