How to use wait () / notify () in Java

I know there are several threads in this thread, but I'm just looking for a VERY INITIAL example of using wait () and notify () in Java. "VERY CHIEF," I mean, just print something. Thanks.

EDIT: this is what I have tried so far and I get an IllegalMonitorStateException:

 public void waiting() { for(int i = 0; i < 10; i++) { if(i == 5) try { this.wait(); } catch (InterruptedException e) { } else System.out.println(i); } System.out.println("notify me now"); this.notify(); } 

code>

+4
source share
6 answers

wait and notify are used in a synchronized block when using threads to pause and resume when it was stopped.

Wait, immediately lose the lock, while Nofity will leave the lock only when the ending bracket occurs.

 public class Mythread implements Runnable{ public synchronized void goo(){ System.out.println("Before Wait"); wait(); System.out.println("After Wait"); } public synchronized void foo(){ System.out.println("Before Notify"); notify(); System.out.println("After Notify"); } public class Test{ public static void main(String[] args){ Thread t = new Thread(new Mythread); t.start(); } } 
+3
source

Your IllegalMonitorStateException exception is due to the fact that you must synchronize the object before calling wait or notify. So,

this.wait

should be

  synchronized(this) { this.wait(); } 

Your example will not run because you will never get to calling notify ... as soon as your thread gets on hold, it will be suspended and will not go any further. You must have two threads to wait / notify for work. One thread pauses when the wait method is called, and finally, the second thread calls synchronized(this) { this.notify() } to make the first thread wake up and continue executing under the wait call.

Synchronization is required because you usually check some condition before waiting, i.e.

  synchronized(this) { if(! this.isReady) { this.wait(); } } 

You need to synchronize to make sure that no other thread changes the state of the isReady flag between the line where you are checking the variable and the line you are waiting in. This way your notification code will be

  synchronized(this) { isReady = true; this.notify(); } 

Now the order of the method calls does not matter. If you first report this, no thread will wake up, but this is normal because you are not going to sleep, since isReady = true. If you go to bed first, isReady = true does nothing, but a notification call wakes up the thread. Finally, synchronization ensures that you do not check the variable in thread A, then set thread B and change the value (do nothing), then try thread A to sleep and never wake up.

Hope this helps.

+2
source

wait() and notify() are used to synchronize threads: a thread can be passed to wait() and will not continue to do anything until it receives a notify() call.

+1
source

The main idea with these functions is that wait() pauses the thread (puts it to sleep), and notify() forces the thread to choose where it left when it was sleeping.

0
source

Take a look at this or just search simple prodcuer consumer problem java on Google. I am sure you will find something to suit your needs.

0
source

See this example on protected blocks from the oracle java site - it includes a processed example, a simple producer and consumer problem .

0
source

All Articles