Skip to content

Instantly share code, notes, and snippets.

@michaelneu
Created May 5, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelneu/f819c6be8fcf4c5323a3 to your computer and use it in GitHub Desktop.
Save michaelneu/f819c6be8fcf4c5323a3 to your computer and use it in GitHub Desktop.
A simple implementation of C#'s BackgroundWorker-class in Java for JavaFX.
import javafx.application.Platform;
/**
* A simple replacement for the C# BackgroundWorker-class for JavaFX
* @author minedev
*/
public abstract class BackgroundWorker {
private Thread thread;
private boolean workerStarted;
private volatile Parameters parameters;
private volatile Object parameter;
private volatile Exception runException;
/**
* Initializes the BackgroundWorker
*/
public BackgroundWorker() {
this.thread = new Thread() {
public void run() {
parameters = new Parameters(parameter, false);
try {
onDoWork(parameters);
} catch (Exception ex) {
runException = ex;
}
try {
Finished finished = new Finished(runException);
Platform.runLater(new Runnable() {
@Override
public void run() {
onWorkerDone(finished);
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
}
/**
* Force the user to implement the behaviour
*/
protected abstract void onDoWork(BackgroundWorker.Parameters args);
protected abstract void onWorkerDone(BackgroundWorker.Finished args);
protected abstract void onProgressChanged(BackgroundWorker.ProgressChanged args);
/**
* Sets the cancelled attribute on the Arguments-object. Used for safely exiting the onDoWork-method
*/
public void cancelWorkerAsync() {
this.parameters.setCancelled();
}
/**
* Run the worker
*/
public synchronized void runWorkerAsync() {
runWorkerAsync(null);
}
/**
* Run the worker and pass a parameter
* @param parameter
*/
public synchronized void runWorkerAsync(Object parameter) {
this.parameter = parameter;
if (!workerStarted && !this.thread.isAlive()) {
workerStarted = true;
this.thread.start();
}
}
/**
* Report progress to the main thread
* @param progress The changed progress
*/
public void reportProgress(int progress) {
reportProgress(progress, null);
}
/**
* Report progress to the main thread and pass an user defined state
* @param progress The changed progress
* @param userState A user defined state
*/
public void reportProgress(int progress, Object userState) {
Platform.runLater(new Runnable() {
@Override
public void run() {
ProgressChanged arguments = new ProgressChanged(progress, userState);
onProgressChanged(arguments);
}
});
}
/**
* Supplies parameters for the onDoWork-method
* @author minedev
*/
public class Parameters {
private final Object parameter;
private volatile boolean cancelled;
public Parameters(Object parameter, boolean cancelled) {
this.parameter = parameter;
this.cancelled = cancelled;
}
/**
* Returns the parameter given in runWorkerAsync
* @return
*/
public Object getParameter() {
return this.parameter;
}
/**
* returns if the worker was cancelled
* @return
*/
public boolean isCancelled() {
return this.cancelled;
}
private void setCancelled() {
this.cancelled = true;
}
}
/**
* Supplies the changed and the user state
* @author minedev
*/
public class ProgressChanged {
private final int progress;
private final Object userState;
public ProgressChanged(int progress, Object userState) {
this.progress = progress;
this.userState = userState;
}
/**
* Returns the changed progress
* @return
*/
public int getProgress() {
return this.progress;
}
/**
* Returns the user defined state
* @return
*/
public Object getUserState() {
return this.userState;
}
}
/**
* Supplies the exception of the onDoWork-method
* @author michael
*
*/
public class Finished {
private final Exception exception;
public Finished(Exception exception) {
this.exception = exception;
}
/**
* Returns the exception
* @return
*/
public Exception getException() {
return this.exception;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment