Skip to content

Instantly share code, notes, and snippets.

@lfaraone
Last active December 21, 2015 23:19
Show Gist options
  • Save lfaraone/6381665 to your computer and use it in GitHub Desktop.
Save lfaraone/6381665 to your computer and use it in GitHub Desktop.
Example of using callbacks in Java This code executes this.doSomething and calls a user-specified callback when its finished. This is a synchronous method, but you could imagine that doSomething forked into a new thread and executed onTaskComplete later.
public interface Callback {
void run(Object result);
}
public class main {
public main() {
this.doSomething(new Callback() {
@Override
public void run(Object result) {
System.out.println(result.hashCode());
}
});
}
public void doSomething(Callback onTaskComplete) {
// some action is performed...
// ...
Object result = new Object();
onTaskComplete.run(result);
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment