[JAVA book list] - JAVA multithreading core technology chapter 3 inter thread communication

This article is a summary of JAVA multithreaded programming core technology. If you want to know the specific details, you can go to the original book.

Chapter 3 inter thread communication

Using wait/notify to realize the communication between threads

  • The function of wait() method is to make the current thread executing code wait. The wait() method is a method of Object class, which is used to put the current thread into the "pre execution queue" and stop execution at the code line where wait() is located until receiving the notice or interrupt. Before calling wait (), the thread must get the Object level lock of the Object, that is, the wait () method can only be invoked in the synchronization method or the synchronization block. After executing the wait () method, the current thread releases the lock.
  • If wait() is called without an appropriate lock, an IllegalMonitorStateException is thrown, which is a subclass of RuntimeException, so there is no need for a try catch statement to catch the exception.
  • Method notify() is also invoked in synchronous or synchronous code blocks, that is, before the call, the thread must also get the object level lock of the object.
  • notify() is used to notify other threads that may wait for the object lock of the object. If there are multiple threads waiting, the thread planner randomly selects a thread in wait state, notifies notify, and causes it to wait for the object lock of the object
  • After executing the notify() method, the object lock will not be released immediately. The current thread will not release the lock until the thread executing the notify() method finishes executing the program.

Thread state

  • The realization of Producer / consumer model

Create an object to store values

public class MyStack {
    private List list = new ArrayList<>();

   synchronized public void push() {
        try{
            while (list.size() == 1) {
                this.wait();
            }
            list.add("anyString=" + Math.random());
            this.notify();
            System.out.println("push=" + list.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public String pop() {
       String returnValue = "";
       try {
           while (list.size() == 0) {
               System.out.println("pop In operation:" + Thread.currentThread().getName() + " Thread presentation wait state");
               this.wait();
           }
           returnValue = "" + list.get(0);
           list.remove(0);
           this.notify();
           System.out.println("pop=" + list.size());
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       return returnValue;
    }
}

Producer

/**
 * Producer
 */
public class P {
    private MyStack myStack;

    public P(MyStack myStack) {
        this.myStack = myStack;
    }


    public void pushService() {
      myStack.push();
    }
}

Producer thread

public class P_Thread extends Thread {
    private P p;

    public P_Thread(P p) {
        this.p = p;
    }

    @Override
    public void run() {
        while (true) {
            p.pushService();
        }
    }
}

Consumer

/**
 * Consumer
 */
public class C {
    private MyStack myStack;

    public C(MyStack myStack) {
        this.myStack = myStack;
    }

    public void popService() {
        System.out.println("pop=" + myStack.pop());
    }
}

Consumer thread

public class C_Thread extends Thread {
    private C r;

    public C_Thread(C r) {
        this.r = r;
    }

    @Override
    public void run() {
        while (true) {
            r.popService();
        }
    }
}

Startup class

public class Run {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        P p1 = new P(myStack);
        P p2 = new P(myStack);
        P p3 = new P(myStack);
        P_Thread pThread1 = new P_Thread(p1);
        P_Thread pThread2 = new P_Thread(p2);
        P_Thread pThread3 = new P_Thread(p3);
        pThread1.start();
        pThread2.start();
        pThread3.start();
        C r1 = new C(myStack);
        C r2 = new C(myStack);
        C r3 = new C(myStack);
        C_Thread cThread1 = new C_Thread(r1);
        C_Thread cThread2 = new C_Thread(r2);
        C_Thread cThread3 = new C_Thread(r3);
        cThread1.start();
        cThread2.start();
        cThread3.start();
    }
}
  • Use of method join
    The function of method join is to make the thread object x normally execute the tasks in the run() method, and to make the current thread z block indefinitely, wait for the thread to destroy, and then execute the code behind the thread z.
  • Use of ThreadLocal class
    The ThreadLocal class mainly solves the problem that each thread binds its own value. The ThreadLocal class can be compared to a box for storing data globally, in which private data of each thread can be stored.
Published 40 original articles, won praise 9, visited 10000+
Private letter follow

Keywords: Java Programming

Added by Master Zen on Thu, 13 Feb 2020 10:29:12 +0200