Last active
December 21, 2015 23:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Callback { | |
void run(Object result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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