Created
November 8, 2019 15:20
-
-
Save h4ck4life/2c628b7fda17bef8292d564385708497 to your computer and use it in GitHub Desktop.
Java simple callback demo using Interface
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 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