The purpose of thread communication is to enable threads to send signals to each other. In addition, thread communication can also cause the thread to wait for signals from other threads. For example, thread B can wait for signals from thread A, which can be a signal that thread A has processed and completed.
Wait() method
- Interrupt the execution of the method, make this thread wait, temporarily give up the right to use the CPU, and allow other threads to use this synchronization method
Notify() method
-Wake up waiting at a certain end of the waiting thread due to the use of this synchronous party
Notifyall() method
Wake up all threads waiting for the end of waiting due to the use of this synchronization method
When to use the wait method
When a variable is used in the synchronization method used by a thread, and this variable needs to be modified by other threads to meet the needs of this thread, then you can use the wait() method in the synchronization method
Here I will take an example in the class as an example to briefly describe the role of wait and notify
We already know that in multi-threading, such as selling tickets, the order of tickets sold in each window is random. If we have 2 ticket selling windows, it is stipulated that 100 tickets must be sold in turn. After selling one ticket in window A, the next ticket must be sold by window B. How do we implement this function?
First of all, I think that if statement can be set. If (i%2==0) then thread 1 runs, otherwise thread 2 runs, but the runs of thread 1 and thread 2 are random, and it cannot be specified to determine who runs by the size of i.
Then we can use wait() and notify() in the thread
After thread 1 is finished, wait a moment, then thread 2 is running, and after thread 2 is finished, then wake up thread 1 and then again
After thread 1 is finished, wait a moment, then thread 2 is run, and after thread 2 is finished, then wake up thread 1
In this way, thread 1 and thread 2 can be run in turn
We have printed 10 numbers as examples to write a program, and built a total of 2 classes Myprint and MyprintTest
Myprint.java
public class Myprint implements Runnable { private int i=0;@Override public void run() {try {print();}catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}}public synchronized void print() throws InterruptedException{while(i<10){System.out.println(Thread.currentThread().getName()+":"+i);i++;notify();try {wait();}catch (InterruptedException e) {e.printStackTrace();}}}}The results are shown in the figure
You can see that thread 1 and thread 2 have been implemented to print alternately with each other.
Just understand the running process
while(i<10){System.out.println(Thread.currentThread().getName()+":"+i);i++;notify();try {wait();}catch (InterruptedException e) {e.printStackTrace();}}}}}When i<10, print thread 1 and then perform wake-up. Since there is no thread in front, this step is not executed. Then thread 1 waits. After executing thread 2, wakes up the previous thread, i.e., thread 1, and then prints thread 1,
This process continues until the loop breaks out, so we can perform rotations
Summarize
The above is the entire content of this article about briefly discussing the role of waiting and notify in thread communication. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!