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

[sdk] Optimize Task memory usage after completion #712

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
27 changes: 20 additions & 7 deletions sdk/src/main/java/pt/ua/dicoogle/sdk/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.slf4j.Logger;

/** An entity for describing an asynchronous task in Dicoogle.
*
* @param <Type> the return type of the FutureTask
Expand All @@ -32,9 +34,11 @@
*/
public class Task<Type> extends FutureTask<Type> {

private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(Task.class);

private final String uid;
private String taskName;
private Callable callable;
private Callable<Type> callable;
private ArrayList<Runnable> toRunWhenComplete;
private LocalDateTime timeCreated;

Expand All @@ -53,22 +57,31 @@ public Task(String uid, String name, Callable<Type> c) {
this.callable = c;
this.uid = uid;
taskName = name;
toRunWhenComplete = new ArrayList<>();
toRunWhenComplete = new ArrayList<>(2);
this.timeCreated = LocalDateTime.now();
}

/** When the task is done, run the runnables registered
* then clean up references to reduce footprint.
*/
@Override
protected void set(Type ret) {
super.set(ret);
for (Runnable r : toRunWhenComplete) {
r.run();
protected void done() {
for (Runnable r : this.toRunWhenComplete) {
try {
r.run();
} catch (Exception ex) {
LOG.warn("Error running task completion hook", ex);
}
}
this.toRunWhenComplete.clear();
this.callable = null;
}

public String getUid() {
return uid;
}

/** Add a completion hook to this task. */
public void onCompletion(Runnable r) {
toRunWhenComplete.add(r);
}
Expand All @@ -92,7 +105,7 @@ public void setName(String name) {
*/
public float getProgress() {
if (callable instanceof ProgressCallable) {
return ((ProgressCallable) this.callable).getProgress();
return ((ProgressCallable<?>) this.callable).getProgress();
}
return -1;
}
Expand Down
Loading