Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read/write to JSONObject with thread-safe #99

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions sdk/src/main/java/com/deploygate/sdk/SdkDeviceStatesCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@

final class SdkDeviceStatesCollector {

private final JSONObject states = new JSONObject();
private final Object mLock;
private final JSONObject states;

SdkDeviceStatesCollector() {
mLock = new Object();
states = new JSONObject();
}

public String getJSONString() {
return states.toString();
synchronized (mLock) {
return states.toString();
}
}

public void collectLocale() {
Expand Down Expand Up @@ -43,8 +51,11 @@ public void collectLocale() {

private void putState(String fqcn, String paramName, Object data) {
String key = String.format("%s$%s", fqcn, paramName);

try {
states.put(key, data);
synchronized (mLock) {
states.put(key, data);
}
} catch (JSONException e) {
Logger.w(e, "Failed to put info: key=%s, value=%s", key, data);
}
Expand Down
Loading