Skip to content

Commit 42b661a

Browse files
committed
Threads for onUpdate and onNotify
1 parent a6a670c commit 42b661a

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,16 +1854,20 @@ public JSONObject update(String key, Object newValue) throws JSONException {
18541854
final Object oldValue = this.opt(key);
18551855
this.put(key, newValue);
18561856

1857-
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1858-
this.propertyChangeSupportUpdate.firePropertyChange(key, oldValue, newValue);
18591857

1860-
this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
1858+
new Thread(() -> {
1859+
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1860+
this.propertyChangeSupportUpdate.firePropertyChange(key, oldValue, newValue);
1861+
this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
1862+
}, "json" + key).start();
18611863

18621864
return this;
18631865
}
18641866

18651867
public JSONObject notify(String key, Object oldValue, Object newValue) throws JSONException {
1866-
this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
1868+
new Thread(() -> {
1869+
this.propertyChangeSupportNotify.firePropertyChange(key, oldValue, newValue);
1870+
}, "json" + key).start();
18671871

18681872
return this;
18691873
}
@@ -1948,19 +1952,21 @@ private JSONObject updateOrRemove(JSONObject jo, boolean remove, boolean trigger
19481952
}
19491953

19501954
if (oldValues.size() > 0) {
1951-
if (triggerUpdate) {
1952-
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
1953-
}
1954-
1955-
oldValues.forEach((key, oldValue) -> {
1956-
final Object v2 = delValues.contains(key) ? null : newValues.get(key);
1957-
final Object v1 = oldValue == null && v2 == null ? JSONObject.NULL : oldValue;
1958-
1955+
new Thread(() -> {
19591956
if (triggerUpdate) {
1960-
this.propertyChangeSupportUpdate.firePropertyChange(key, v1, v2);
1957+
this.propertyChangeSupportUpdate.firePropertyChange(JSONObject.propertyChangeGlobalKeyword, oldThis, this);
19611958
}
1962-
this.propertyChangeSupportNotify.firePropertyChange(key, v1, v2);
1963-
});
1959+
1960+
oldValues.forEach((key, oldValue) -> {
1961+
final Object v2 = delValues.contains(key) ? null : newValues.get(key);
1962+
final Object v1 = oldValue == null && v2 == null ? JSONObject.NULL : oldValue;
1963+
1964+
if (triggerUpdate) {
1965+
this.propertyChangeSupportUpdate.firePropertyChange(key, v1, v2);
1966+
}
1967+
this.propertyChangeSupportNotify.firePropertyChange(key, v1, v2);
1968+
});
1969+
}, "json_GLOBAL").start();
19641970
}
19651971

19661972
return this;

src/test/java/org/json/junit/JSONTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ public void updateListenerGlobalTest() {
473473
j.update("myMapListener", "propertyChange456");
474474
j.update("myMapListener", "propertyChange789");
475475

476+
try {
477+
Thread.sleep(1L);
478+
} catch (InterruptedException ex) {
479+
ex.printStackTrace();
480+
}
481+
476482
assertEquals(counter.get(), globalExecutions.get() * 3);
477483
}
478484

0 commit comments

Comments
 (0)