![]() ![]() ScheduledExecutorService is an interface provided by Java as a precise solution to the delay problem. Last but Least: The ExecutorService based Scheduler To solve this problem Java gave a great solution in terms of Scheduler. For Ex: we are calling an API whose result might be delayed, in such cases above approach would fail to give proper response. ![]() Now all out above solutions are either pausing the execution for a specified amount of time, but what about cases when we are ourselves unsure of the exact wait time. It happened due to wait() which paused the main() thread till the t1 thread executed it’s method and called the notify() method. Waiting For the Thread t1 to complete its execution: It simply directs the current thread to sleep for a specific time. This method is present in the Thread class. The Most Basic Approach: Thread’s class Sleep() MethodĪs the name suggests, sleep method is a quick but a dirty approach to execute the delay in Java. The Java’s API provides methods for this functionality. So now if we want to use delay the only possible way is pause the execution of Threads. Having knowledge of threads you would be for sure aware about main Thread, the thread in which main function is called. To understand the concept of delay we need to understand about Threads in Java, if you are aware about it you can continue reading otherwise we suggest you learn about threads once before moving ahead. Like any other programming language, Java supports delays. In such cases we have to use delay in our code. ![]() We are aware that calling certain API like Google Maps API would take at least 5-8 seconds of time for fetching a response. Before we move on further to understand the working of delay in Java, let’s understand some practical scenario in which we would need a delay in execution.Īs soon as the application is loaded and the user logged in, we want to fetch the current location of the user. Let’s discuss about scenario when we want to introduce a delay in execution of a subsequent program. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |