Skip to content

Instantly share code, notes, and snippets.

@h4ck4life
Created November 8, 2019 15:20
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 h4ck4life/2c628b7fda17bef8292d564385708497 to your computer and use it in GitHub Desktop.
Save h4ck4life/2c628b7fda17bef8292d564385708497 to your computer and use it in GitHub Desktop.
Java simple callback demo using Interface
public class CallbackDemo {
public static void main(String[] args) throws InterruptedException {
RestApiCall restApiCall = new RestApiCall();
restApiCall.getUsersAsync(new UserListener() {
@Override
public void onDataReceived(String data) {
System.out.println(data);
}
});
restApiCall.getUsersSync(new UserListener() {
@Override
public void onDataReceived(String data) {
System.out.println(data);
}
});
}
interface UserListener {
public void onDataReceived(String data);
}
static class RestApiCall {
public void getUsersSync(UserListener listner) throws InterruptedException {
Thread.sleep(5000);
listner.onDataReceived("alif here sync");
}
public void getUsersAsync(UserListener listener) {
new Thread(() -> {
try {
Thread.sleep(15000);
listener.onDataReceived("alif here async");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment