Preface
The use of multithreading must involve communication between threads
1. Waiting/notification mechanism
Do not use wait/notify mechanism to achieve inter-thread communication
Example: when the infinite while loop is used with the sleep() method
Reason: Continuously using the while statement to poll for a certain condition will waste the CPU. Small polling interval, more waste of cpu resources
Solution: realize the communication between threads, use the wait/notify mechanism
Introduction to waiting notification mechanism
Example: The chef prepares dishes, puts them at the window, and the waiter serves the dishes. When the chef is cooking, the waiter is waiting; the chef puts the food at the window to notify.
Question: In the first two chapters, multiple threads access the same variable at the same time, is it a wait/notify mechanism?
No, multi-threaded access to the same variable at the same time is actively read by the thread, and it is possible that the data read has been changed and is not the desired data.
Implementation of waiting/notifying mechanism
Implementation: use wait() method and notify()/notifyAll method
wait: Make the thread currently executing code wait until notified or interrupted. There must be an object lock before execution, and if not, an exception will be reported; after the wait is executed, the current thread releases the lock, and before returning from wait, it competes with other threads to re-acquire the lock.
notify: call in a synchronized method or synchronized block. Before calling, there must be a lock. If there is no lock, an exception will be reported. After the notify is executed, the object lock will not be released immediately, and the thread executing the method will release the program after the program is executed.
notifyAll: Make all threads waiting for the same shared resource in the waiting queue exit from the waiting state and enter the running state. The order of execution depends on the implementation of the JVM.
State transition diagram:
Thread enters Runnable situation:
The thread enters the blocking state:
Compare wait and sleep?
Wait is for the object, and there is a process of unlocking and locking during execution
Sleep corresponds to a thread, there will be no unlocking and locking process.
wait method
In the wait state, an exception will be reported when interrupt is executed. Why?
Use of method wait (long):
1. The parameter is a time. After this time, the method automatically releases the wait state and starts running.
2. Or within this time, awakened by other threads.
notify and notifyAll methods
The notify method only wakes up one thread randomly, and is called many times, many times randomly.
Use the notifyAll method to wake up all threads.
Confusing program logic
1. Early notification, disrupting the program logic
2. Waiting conditions change
Producer consumer model
One producer, one consumer or multiple producer consumers
Terrible suspended animation
Multi-producer to multi-consumer, it is easy to enter the suspended animation state, for example, the producer wakes up the producer and the consumer wakes up the consumer. If there are too many such cases, it will be suspended.
Example: In a whil cycle, producer 1 notifies consumer 1, but it is too early, so the lock is released before the operation is complete. Continue the cycle, find that it has not been consumed, and enter the waiting state. Producer 2 is activated, and so on. . .
Solve suspended animation
1. Operating value: one production to one consumption. Change notify to notifyAll
2. Operation stack: one production to one consumption, one production and more consumption, more production and one consumption, more production and more consumption.
Get data from the List stack to make the maximum capacity of List 1
Inter-thread communication
Use pipe flow. One thread sends data to the input pipe, and the other thread reads from the input pipe.
1. Byte stream: PipedReader, PipedOutputStream
2. Character stream: PipedReader, PipedWriter
Second, the use of join method
join method: waiting for the thread object to be destroyed
Principle: Wait for the child thread to finish running before ending. Use the wait method internally to wait.
Problem: Join and interrupt will be abnormal because the object of the current thread is interrupted
Use of join (long)
1. The parameter is still the set waiting time
What is the difference between join (long) and sleep (long)?
Lock release
Three, the use of class ThreadLocal
It is a box that stores data globally, and the private data of each thread can be stored in the box
Role: to make thread variables have isolation
Fourth, the class InheritableThreadLocal
You can let the child thread get the value of the parent thread
If the parent thread changes the value when the child thread takes the value, the child thread gets the same value.