Java Object Wait Timeout
# Java Object wait(long timeout) Method\\n\\n[Java Object Class](#)\\n\\n* * *\\n\\nThe Object wait(long timeout) method causes the current thread to wait (block) until another thread invokes the notify() method or notifyAll() method of this object, or the timeout specified by the timeout parameter elapses.\\n\\nIf the timeout parameter is 0, there is no timeout and the thread will wait indefinitely, similar to the wait() method.\\n\\nThe current thread must be the monitor owner of this object, otherwise an IllegalMonitorStateException will be thrown.\\n\\nIf the current thread is interrupted by any thread before or during waiting, an InterruptedException will be thrown.\\n\\nIf an illegal argument is passed, an IllegalArgumentException will be thrown.\\n\\n### Syntax\\n\\npublic final void wait(long timeout)\\n\\n### Parameters\\n\\n* **timeout** - The waiting time in milliseconds.\\n\\n### Return Value\\n\\nNo return value.\\n\\n### Instance\\n\\nThe following example demonstrates the use of wait(long timeout) method:\\n\\n## Instance\\n\\n```java\\nimport java.util.Collections;\\nimport java.util.LinkedList;\\nimport java.util.List;\\n\\npublic class TutorialTest extends Object{\\n private List synchedList;\\n\\n public TutorialTest(){\\n // Create a synchronized list\\n synchedList = Collections.synchronizedList(new LinkedList());\\n }\\n\\n // Remove list elements\\n public String removeElement() throws InterruptedException {\\n synchronized (synchedList) {\\n // Conditions for thread waiting\\n while (synchedList.isEmpty()) {\\n System.out.println("List is empty, waiting");\\n synchedList.wait();\\n System.out.println("Waiting thread resumed");\\n }\\n String element = (String) synchedList.remove(0);\\n System.out.println("Element removed: " + element);\\n return element;\\n }\\n }\\n\\n // Add element\\n public void addElement(String element) {\\n System.out.println("Opening thread");\\n synchronized (synchedList) {\\n // Add elementAdd to the synchronized list and notify other threads\\n synchedList.add(element);\\n System.out.println("Element added: " + element);\\n synchedList.notifyAll();\\n }\\n }\\n\\n public static void main(String[] args) {\\n final TutorialTest demo = new TutorialTest();\\n\\n Runnable runA = new Runnable() {\\n public void run() {\\n try {\\n String item = demo.removeElement();\\n Thread.sleep(50);\\n } catch (InterruptedException ex) {\\n ex.printStackTrace();\\n }\\n }\\n };\\n\\n Runnable runB = new Runnable() {\\n public void run() {\\n demo.addElement("Hello");\\n System.out.println("Element added");\\n }\\n };\\n\\n try {\\n Thread threadA1 = new Thread(runA, "Google");\\n threadA1.start();\\n Thread.sleep(100);\\n Thread threadB = new Thread(runB, "Tutorial");\\n threadB.start();\\n threadA1.join();\\n threadB.join();\\n } catch (InterruptedException ex) {\\n ex.printStackTrace();\\n }\\n }\\n}\\n\\nThe output of the above code example is:\\n\\nList is empty, waiting\\nOpening thread\\nElement added: Hello\\nElement added\\nWaiting thread resumed\\nElement removed: Hello
YouTip