Wait (), Policy (), policyall ()Does not belong
Thread class, But belongs
Object base classIn other words, each object has
Functions of wait (), Y (), and notifyAll ()Because each object has a lock, the lock is the basis of each object, and of course the method to operate the lock is also the most basic.
Wait causes the current thread to wait until other threads call this objectNotify ()Method orPolicyall ()Method, or be interrupted by other threads. Wait can only be called by a thread holding a lock on the image.
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 (random ). The wake-up thread can continue to be executed until the current thread abandons the lock on this object. Like the Wait method, policy can only be called by a lock-like thread. policyall is the same. The difference is that policyall calls all the threads waiting to lock this object.
"It can only be called by the thread holding the lock on the image." This indicates that the wait and Y methods must be executed in the synchronization block, that is, within synchronized (obj. in addition, the synchronized code block does not have a lock, so the thread must obtain the lock to continue execution. Complement each other.
Let's look at a classic example (producer and consumer ):
The first is the consumer Thread class:
Copy codeThe Code is as follows: import java. 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 (); // The container is empty. Discard the lock and wait for production.
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
Container. remove (0 );
Container. Y ();
System. out. println ("I have eaten" + (++ count) + ");
}
}
}
}
Next is the producer Thread class:Copy codeThe Code is as follows: import java. 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 ){
// If the container exceeds the maximum value, do not wait for consumption in production.
Try {
Container. wait ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
Container. add (new Object ());
Container. Y ();
System. out. println ("I have produced" + (++ count) + ");
}
}
}
}
The last is the test class:Copy codeThe Code is as follows: import java. util. ArrayList;
Import java. util. List;
Public class MultiThread {
Private List container = new ArrayList ();
Public final static int MAX = 5;
Public static void main (String args []) {
MultiThread m = new MultiThread ();
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;
}
}
The running result is as follows:Copy codeThe Code is as follows: I have produced 1
I ate one
I have produced two
I have produced three
I have produced four
I have produced five
I have produced 6
I have produced 7
I have eaten 2
I have produced 8
I have eaten 3
I have produced 9
I ate four
I ate five
I ate 6
I have eaten 7
I ate eight
I have produced 10
I have produced 11
I ate nine
I have produced 12
I have eaten 10
......