When a thread is running inside the system, the program usually does not control the rotation of the thread accurately, but there are mechanisms to ensure that the thread is running in a coordinated way.
Reconciling threads by the synchronization monitor object
This functionality can be accomplished by using the wait (), notify (), Notifyall () Three methods provided by the object class (note that these three methods belong to the object class and do not belong to the thread class). These three methods must be called by the synchronization monitor and can be divided into two scenarios:
- For synchronous methods, the synchronization monitor defaults to the current instance (this), so you can call these three methods directly in the synchronous method;
- For synchronous code blocks, the synchronization monitor is an object in the synchronized bracket, and these three methods must be called using the object.
The following three methods are introduced:
Wait (): Causes the current thread to wait (by running into blocking) until the other thread calls the synchronization Monitor's notify () or Notifyall () method to wake the thread. The wait () method causes the current thread to release the lock on the synchronization monitor;
Notify (): A thread waiting on the synchronization monitor to wake up (by blocking into readiness). If multiple threads are waiting on this synchronization monitor, one of the threads is awakened at will. A thread that is awakened cannot be executed immediately and requires the current thread to release the lock on the synchronization monitor before it can;
Notifyall: Wakes up all the threads waiting on the synchronization monitor. A thread that is also awakened cannot be executed immediately, requiring the current thread to release the lock on the synchronization monitor before it can get an opportunity to execute.
Using conditional variables to coordinate control threads
When a thread uses a synchronous lock for thread synchronization, there is no implicit synchronization monitoring object in the system, and Wait (), notify (), Notifyall () are not used to reconcile the thread.
When the lock object is used to ensure thread synchronization, Java provides the condition class to coordinate thread communication. The condition instance is essentially bound to the lock object, and the Newcondition () method of the object is used to obtain the condition instance of the particular lock.
Condition provides await (), signal (), and Signalall () for thread coordination. These three methods correspond to the wait (), notify (), and Notifyall () methods of the synchronization monitor object, except that the caller is different.
Thread communication using pipeline flow
Pipeline flow is divided into three main categories: Pipeline byte stream (PipedInputStream and PipedOutputStream), Pipe character streams (Pipedreader and PipedWriter) and new IO pipelines (Pipe.sinkchannel and Pipe.sourcechannel).
Using pipeline flow for thread communication can be done as follows:
- Use the new keyword to create pipelines and pipe output streams respectively;
- Connect two input and output streams using the Connect method of the pipeline to the stream or the pipeline output flow;
- The pipeline input stream and pipeline output stream are passed into two threads respectively;
- Two threads rely on their own pipeline input stream, pipeline output stream, respectively, to communicate.
The best way to communicate with less pipeline flow is to use thread-sharing data.
Java Multithreading-Thread communication