Skip to content

Commit

Permalink
AutoSave is always done in an own thread
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jan 7, 2024
1 parent 198df97 commit b7eb809
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/main/java/de/nqueensfaf/Solver.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public abstract class Solver {
private OnUpdateConsumer onUpdateConsumer;
private Consumer<Solver> initCb, finishCb;
private int solutionsSmallN = 0;
private boolean isStoring = false;
private boolean isSaving = false;

protected Solver() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
while (isStoring) {
while (isSaving) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -115,8 +115,7 @@ private void preconditions() {
private Thread backgroundThread(boolean updateConsumer, boolean autoSaver) {
return new Thread(() -> {
// for autoSaver
String filePath = config().autoSavePath;
filePath = filePath.replaceAll("\\{n\\}", "" + n);
final String filePath = config().autoSavePath.replaceAll("\\{n\\}", "" + n);
float progress = getProgress() * 100;
float tmpProgress = progress;

Expand All @@ -130,11 +129,13 @@ private Thread backgroundThread(boolean updateConsumer, boolean autoSaver) {
if (progress >= 100)
break;
else if (progress >= tmpProgress + config().autoSavePercentageStep) {
try {
save(filePath);
} catch (IllegalArgumentException | IOException e) {
System.err.println("error in autosaver thread: " + e.getMessage());
}
new Thread(() -> {
try {
save(filePath);
} catch (IllegalArgumentException | IOException e) {
System.err.println("error in autosaver thread: " + e.getMessage());
}
}).start();
tmpProgress = progress;
}
}
Expand Down Expand Up @@ -192,11 +193,11 @@ private void smallBoardNQ(int ld, int rd, int col, int row, int free, int mask)
}

public final void save(String filepath) throws IOException, IllegalArgumentException {
if(isStoring)
if(isSaving)
return;
isStoring = true;
isSaving = true;
save_(filepath);
isStoring = false;
isSaving = false;
}

public final synchronized void load(File file)
Expand Down

0 comments on commit b7eb809

Please sign in to comment.