Skip to content

Commit

Permalink
Use synchronized for YAML write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Apr 10, 2020
1 parent 1eb161b commit a04b15f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
Expand Up @@ -20,14 +20,14 @@ public AsyncConsolidator(Runnable action) {
/**
* Call this before running each task
*/
public void reserve() {
public synchronized void reserve() {
++tasks;
}

/**
* Call this after running each task
*/
public void release() {
public synchronized void release() {
if (tasks > 0) --tasks;
if (!done && tasks <= 0) {
done = true;
Expand Down
Expand Up @@ -64,7 +64,7 @@ public void set(YAMLSection value) {
* @throws IOException
*/
@SuppressWarnings("unchecked")
public void reload() throws IOException {
public synchronized void reload() throws IOException {
if (file.exists()) {
InputStream stream = new FileInputStream(file);
this.config = new YAMLSection((LinkedHashMap<String, ?>) yaml.loadAs(stream, LinkedHashMap.class), null, null, yaml);
Expand All @@ -80,10 +80,13 @@ public void reload() throws IOException {
* @throws IOException
*/
public void save() throws IOException {
if (!file.exists()) file.createNewFile();
FileWriter writer = new FileWriter(file);
yaml.dump(Util.getDespiteException(() -> Util.reflect(ObjectMap.class.getDeclaredField("map"), config), null), writer);
writer.close();
final YAMLSection config = this.config;
synchronized (config) {
if (!file.exists()) file.createNewFile();
FileWriter writer = new FileWriter(file);
yaml.dump(Util.getDespiteException(() -> Util.reflect(ObjectMap.class.getDeclaredField("map"), config), null), writer);
writer.close();
}
}

@Override
Expand All @@ -102,10 +105,10 @@ public String toString() {

static DumperOptions getDumperOptions() {
DumperOptions options = new DumperOptions();
options.setAllowUnicode(false);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setSplitLines(false);
options.setIndent(2);
Util.isException(() -> options.setAllowUnicode(false));
Util.isException(() -> options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK));
Util.isException(() -> options.setSplitLines(false));
Util.isException(() -> options.setIndent(2));

return options;
}
Expand Down
Expand Up @@ -104,7 +104,7 @@ public YAMLSection clone() {
}

@Override
public String toString() {
public synchronized String toString() {
return yaml.dump(map);
}

Expand Down
Expand Up @@ -134,7 +134,7 @@ private Object convert(Object value) {
* @param handle Handle
* @param value Value
*/
public void set(K handle, Object value) {
public synchronized void set(K handle, Object value) {
if (Util.isNull(handle)) throw new NullPointerException();
map.put(handle, convert(value));

Expand Down Expand Up @@ -203,7 +203,7 @@ public void safeSetAll(ObjectMap<? extends K> values) {
*
* @param handle Handle
*/
public void remove(K handle) {
public synchronized void remove(K handle) {
if (Util.isNull(handle)) throw new NullPointerException();
map.remove(handle);

Expand Down

0 comments on commit a04b15f

Please sign in to comment.