This is the 7th day of my participation in the August More Text Challenge

Thread communication

  • The purpose of thread communication is to enable threads to communicate with each other by sending signals, while thread signals can wait for the state from other signals. For example, thread A receives a signal from thread B that the data operated by thread B is ready for further processing.

The way a thread signal is sent

Threads communicate by sharing variable values

  • willstatusAs shared variables, threads A and B pass throughsynchronizedThe modified method modifies and queries variable values. When the value of the variable changes, that is, the completion status of another thread is detected and the subsequent operation of the thread is performed.
    private boolean status = false;

    public synchronized boolean isStatus() {
        return status;
    }

    public synchronized void setStatus(boolean status) {
        this.status = status;
    }
Copy the code
  • Note that this approach requires that the shared variable instances taken by different threads be the same.

Busy wait when threads share variable values

while(isStatus()){
    //do something
}
Copy the code

– When thread A waits for thread B, the status process becomes the busy wait of the thread. Note that thread A needs to use a while statement instead of an if statement to ensure that it executes correctly when data changes. Otherwise, statements in thread AIF will not execute due to time slice switching.

Communicate via wait(), notify(), and notifyAll() built-in methods

  • Thread waiting is judged when CPU time slice is switched, which is not efficient utilization of CPU. If you can use a publish-subscribe approach, wake up while the thread is executing is an ideal way to communicate.
  • Java provides a similar mechanism that allows threads to become inactive while waiting. To implement this mechanism,java.lang.ObjectprovidesWait (), notify(), and notifyAll()Three ways.
  • Thread A can wait until another thread becomes active when it calls notify. All this assumes that the thread acquires the lock on the object in the first place. That is, wait and notify must be insynchronizedThis will only work if executed in a block of code.IllegalMonitorStateException. whilenotifyAllThe method is to wake up the wait state of all threads, creating a scramble.
  • As for why it has to be theresynchronizedCode block execution, mainly to ensure that the thread execution order. If two threads are not addedsynchronizedProbablynotifyCode beforewaitCode execution, and this leads towaitCode can never be woken up.

False awaken

  • A false wake up occurs when a thread is woken up when it should not be.
  • False awakenings are intended to avoid operations such as lock wait by using while instead of if.
  • Reasons why while can avoid false wake up:
If (a == 1){//1. Wait for lock.wait() //2. 3. Execute logic // do something //4. Notifies other threads lock.notifyall () // use while while(a == 1){//1. Wait to execute lock.wait() //2. Execute irrelevant logic // print} //3. Execute logic == cannot execute // do something //4 Notifies other threads == Cannot execute Lock.notifyall () due to added while judgmentCopy the code
  • As you can see from the code above, the secondary judgment of the while can avoid the related exception in the false wake up.

Afterword.

  • How many events through the ages? Leisurely. The Yangtze River is still rolling.