![]() ![]() wait(long timeoutMillis) method: The other two variances put the current thread in wait for a specific amount of time before they wake up. We discussed this method in the above example.Ģ. wait() method: This method doesn’t take any argument it waits indefinitely for any other thread to call notify()or notifyAll() method on the object to wake up the current thread. Object class has three variances of the wait() method. Second thread for Add pages in printerĪfter called notify() method number of Paper : 200 create two new thread and start them simultaneously Synchronized void addPages(int noOfPages) ("After called notify() method number of Paper : " + this.noOfPaper) ("Number of Papers in printer are less") then wait for addPages() synchronized method If balance number of Papers are less than user input Synchronized void printingPages(int pages) ![]() Synchronized the method for inter-thread communication If the number of papers in the printer is less than the number of entered input, then we will call the wait() method for the thread, meanwhile, another thread will add more pages in the printer and notify the current thread by notify() method. So, we are creating two threads, one thread for printing the pages and another thread for adding the pages to the printer. Let’s say a user wants to print some pages in the printer. Unlike the sleep() method, in the wait() method, the thread goes in waiting state and it won’t come back automatically until we call the notify()or notifyAll(). The wait() method is tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism.ģ. So that multiple threads can communicate between the thread by use of use wait() and notify (). You can tell one thread to stop working (By wait() method) from another thread based upon some condition, later you can notify it to start processing again(By notify() or notifyAll() method).Ģ. By use of the wait() method a thread is paused(release lock) running in its critical section and another thread can enter (acquire the lock) in the same critical section. As you know in a synchronized block or method, only one thread can enter(acquire the lock). In Inter-thread communication, the synchronized threads can communicate with each other. ![]() The wait() method is used for interthread communication. public final void wait() throws InterruptedExceptionġ. ![]() It is a final method, so we can’t override it. This method tells the calling thread (Current thread) to give up the lock and go to sleep until some other thread enters the same monitor and calls notify() or notifyAll(). The wait() method is defined in the Object class which is the super most class in Java. wait(long timeoutMillis) method? What is the wait() method in Java? Here is the table content of the article will we will cover this topic.ģ. It is the most common question in interviews. Here we will discuss the wait() method in Java. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |