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.
![java timer utility pause resum java timer utility pause resum](http://www.macyourself.com/wp-content/uploads/2011/02/022011-pauseprocess-screen1.jpg)
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.
![java timer utility pause resum java timer utility pause resum](http://infologic.co.uk/products/midp/et/editintervalSE.gif)
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.