Skip to content

Commit 80f89d3

Browse files
Copilotriccardobl
andcommitted
Fix race condition in getWorker using computeIfAbsent
Co-authored-by: riccardobl <4943530+riccardobl@users.noreply.github.com>
1 parent 53e97fd commit 80f89d3

2 files changed

Lines changed: 6 additions & 12 deletions

File tree

jme3-android/src/main/java/com/jme3/app/state/VideoRecorderAppState.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ private String getResolutionKey(int w, int h) {
277277

278278
private ResolutionWorker getWorker(int w, int h) {
279279
String key = getResolutionKey(w, h);
280-
ResolutionWorker worker = workers.get(key);
281-
if (worker == null) {
280+
return workers.computeIfAbsent(key, k -> {
282281
// Generate filename for this resolution
283282
File workerFile;
284283
if (file == null) {
@@ -291,10 +290,8 @@ private ResolutionWorker getWorker(int w, int h) {
291290
String extension = dotIndex > 0 ? originalPath.substring(dotIndex) : ".avi";
292291
workerFile = new File(basePath + "-" + w + "x" + h + "-" + (System.currentTimeMillis() / 1000) + extension);
293292
}
294-
worker = new ResolutionWorker(w, h, workerFile);
295-
workers.put(key, worker);
296-
}
297-
return worker;
293+
return new ResolutionWorker(w, h, workerFile);
294+
});
298295
}
299296

300297
public void addImage(Renderer renderer, FrameBuffer out) {

jme3-desktop/src/main/java/com/jme3/app/state/VideoRecorderAppState.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ private String getResolutionKey(int w, int h) {
267267

268268
private ResolutionWorker getWorker(int w, int h) {
269269
String key = getResolutionKey(w, h);
270-
ResolutionWorker worker = workers.get(key);
271-
if (worker == null) {
270+
return workers.computeIfAbsent(key, k -> {
272271
// Generate filename for this resolution
273272
File workerFile;
274273
if (file == null) {
@@ -281,10 +280,8 @@ private ResolutionWorker getWorker(int w, int h) {
281280
String extension = dotIndex > 0 ? originalPath.substring(dotIndex) : ".avi";
282281
workerFile = new File(basePath + "-" + w + "x" + h + "-" + (System.currentTimeMillis() / 1000) + extension);
283282
}
284-
worker = new ResolutionWorker(w, h, workerFile);
285-
workers.put(key, worker);
286-
}
287-
return worker;
283+
return new ResolutionWorker(w, h, workerFile);
284+
});
288285
}
289286

290287
public void addImage(Renderer renderer, FrameBuffer out) {

0 commit comments

Comments
 (0)