Java multi-thread wait (), Y (), policyall ()
Wait (), Policy (), policyall () does not belong to the Thread class, but belongs to the object base class. That is to say, each object has wait (), Policy (), policyall ()
Because all objects have locks, the lock is the basis of each object, and of course the lock operation method is also the most basic.
Let's take a look at how Java Doc says:
Wait causes the current thread to wait until other threads call the notify () method or notifyall () method of this object. The current thread must own this object monitor. This thread releases ownership of this monitor and waits until other threads wake up on the monitor of this object by calling the notify method or the notifyall method. Then the thread will wait until it obtains ownership of the monitor again before it can continue execution.
Notify wakes up a single thread waiting on this object monitor. If all threads are waiting on this object, the system will wake up one of the threads. The wake-up thread can continue to be executed until the current thread abandons the lock on this object. This method should be called only by the thread that is the owner of this object monitor.
"The current thread must own this object monitor" and "This method should be called only by the thread that is the owner of this object monitor." This indicates that the wait and notify methods must be executed in the synchronization block, that is, synchronized (within OBJ ).
After the wait method is called, the current thread releases the lock and enters the waiting state. until other threads (or only other threads) use the y method or notifyall. this thread re-acquires the image lock.
Continue execution. Remember that the thread must obtain the lock to continue execution. Because synchronizedCodeThere is no lock in the block and it cannot be moved. Let's look at a classic example:
Code
Package Productandconsume;
ImportJava. util. List;
Public Class Consume Implements Runnable {
Private List container = Null ;
Private Int Count;
Public Consume (list lst ){
This . Container = LST;
}
Public Void Run (){
While ( True ){
Synchronized (Container ){
If (Container. Size () = 0 ){
Try {
Container. Wait (); // Discard lock
} Catch (Interruptedexception e ){
E. printstacktrace ();
}
}
Try {
Thread. Sleep ( 100 );
} Catch (Interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Container. Remove ( 0 );
Container. Y ();
System. Out. println ( " I have eaten " + ( ++ Count) + " Items " );
}
}
}
}
PackageProductandconsume;
ImportJava. util. List;
Public Class Product Implements Runnable {
Private List container = Null ;
Private Int Count;
Public Product (list lst ){
This . Container = LST;
}
Public Void Run (){
While ( True ){
Synchronized (Container ){
If (Container. Size () > Multithread. max ){
Try {
Container. Wait ();
} Catch (Interruptedexception e ){
E. printstacktrace ();
}
}
Try {
Thread. Sleep ( 100 );
} Catch (Interruptedexception e ){
E. printstacktrace ();
}
Container. Add ( New Object ());
Container. Y ();
System. Out. println ( " I have produced " + ( ++ Count) + " Items " );
}
}
}
}
PackageProductandconsume;
ImportJava. util. arraylist;
ImportJava. util. List;
Public ClassMultithread {
PrivateList container= NewArraylist ();
Public final static int max = 5;
Public static void main (string ARGs []) {
Multithread m= NewMultithread ();
New Thread ( New Consume (M. getcontainer (). Start ();
New Thread ( New Product (M. getcontainer (). Start ();
New Thread ( New Consume (M. getcontainer (). Start ();
New Thread ( New Product (M. getcontainer (). Start ();
}
Public List getcontainer (){
Return Container;
}
Public void setcontainer (list container) {
This . container = container;
}< br>