At any one time, the object's control (monitor) can only be owned by one thread.
Whether you are executing an object's wait, notify, or Notifyall method, you must ensure that the currently running thread has control over the object (monitor)
If the above three methods of the object are executed in a thread without control, the java.lang.IllegalMonitorStateException exception is reported.
The JVM is multi-threaded and does not guarantee the timing of runtime threads by default
Illegalmonitorstateexception wait or notify have to make sure they get the lock on the object.
If you do not use wait¬ify, the thread will always try to occupy the lock, wasting performance.
Wait will release the lock but notify will release it? What happens if it is not released.
Will not be released if you release the thread to run halfway there will be a problem until the entire synchronization statement is self-completed before it is released.
package org.famous.unyielding.current;import java.util.vector;public class client { Public static void main (String[] args) {int goods = 0;Object Plock = new object (); Vector<string> message = new vector<string> (); Thread customer = new thread (New customer (message, "Customer", pLock)); Thread producter = new thread (New producter (message, "Producter", pLock)); Customer.start ();p Roducter.start ();}} package org.famous.unyielding.current;import java.util.vector;/** * * @author patzheng * */public class Customer implements Runnable {private Vector<string> messages = new vector ();p rivate string threadname;private object plock;public customer (VECTOR<STRING>&NBSP;MESSAGES,&NBSP;STRING&NBsp;threadname, object plock) {this.messages = messages;this.threadName = Threadname;this.plock = plock;} @Overridepublic void run () {thread.currentthread (). SetName (ThreadName); System.err.println (Thread.CurrentThread (). GetName () + "Goods ' S size" + messages.size ( ));synchronized (Plock) {while (True) {if (Messages.size () <= 0) { Try {plock.wait (); System.err.println ("customer wait");} catch (interruptedexception e) {e.printstacktrace ();}} else {system.err.println (Thread.CurrentThread (). GetName () + "Goods ' S size" + messages.size ()); System.err.println ("Customer notify producer"); messages.remove (0); try {thread.sleep (1000);} catch (interruptedexception e) {e.printstacktrace ();} Plock.notify ();}}}} Package org.famous.unyielding.current;import java.util.date;import java.util.vector;public class producter implements runnable {private Vector<string> messages = new vector ();p rivate string threadname;private object plock;public producter (Vector<string> messages, string threadname, object plock) {this.messages = messages;this.threadName = threadName; This.plock = plock;} @Overridepublic void run () {thread.currentthread (). SetName (ThreadName); System.err.println (Thread.CurrentThread (). GetName () + "Goods ' S size" + messages.size ( ));synchronized (Plock) {while (True) {if (Messages.size () >= 1) { Try {system.err.println ("producer wait");p lock.wait ();} catch (interruptedexception e) {e.printstacktrace ();}} else {messages.add (New date (). toString ()); System.err.println (Thread.currentthreaD (). GetName () + "Goods ' S size" + messages.size ()); System.err.println ("Producer notify customer"); Try {thread.sleep (1000);} catch (interruptedexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();} Plock.notify ();}}}}
JAVA Threading Basics Summary