Java Concurrency arts study notes (five)

Java Concurrency basis (b) - Inter-thread communication

1. synchronized and volatile keywords

Principle 1.1 implementation of inter-thread communication

This form of communication is the use of volatile and synchronization mechanisms synchronized by the read / write shared variables to achieve inter-thread communication. When a shared variable is modified, the synchronization mechanism so that it can be seen by other threads, if one thread to modify the shared values ​​of the variables tell another thread, which would achieve the communication between threads on logic.

1.2 Features

This form of communication can not be designated to receive the thread, since when the shared variable has been modified, selected another thread is random accessed first. In general, this communication system is not transmitting data, but in the shared data.

2. Wait / notification mechanism

2.1 wait associated methods / notifications

Method name effect
wait() This method is called a thread into a wait state of the thread, only to wait another thread or interrupt notification to return from the method. In addition, after calling the method releases the object lock
wait(long) The method is to wait for a timeout version wait () method. And that the role of the long wait in milliseconds () consistent method, or if the notification has not been interrupted, the process directly returns after a long time exceeded.
notify() Another notification waiting thread on the object, so that it () method returns from the wait. This method is called a thread must first obtain the object lock.
notifyAll() Also notice that all threads waiting on the object, so that they return from wait () method. This method is called a thread must first obtain the object lock.

2.2 waiting understand / notification mechanism

Wait for notification mechanism, it refers to a thread A calls after a certain object O wait () method into a wait state thread. After another thread object O B calls notify () or notifyAll () method returns the thread from A wait () method. These two threads is achieved through the interaction of the same object O.

2.3 Waiting / Notes notification mechanism

● Call wait (), notify () or notify the front () method must first obtain a lock object.

● thread calls wait () method of the thread enters the wait state, and placed into the queue while release the object lock.

● Another thread calls notify () method after waiting queue by a thread waiting state into the blocking state, and is moved to the synchronous queue. Similarly notifyAll () method of operation is all the threads waiting queue.

● When the thread calls notify () or notifyAll () method, another thread does not () method returns immediately from the wait, but you need to call notify () or notifyAll () thread releases the object lock after a chance from the wait () method returns.

● premise returned from the wait () method is to obtain the object lock.

2.4 Waiting / classic paradigm notification mechanism

● Wait side:

synchronized(对象O) {
    while(条件不成立) {
        O.wait();
    }
    相应的处理逻辑;
}
复制代码

● Notify Party:

synchronized(对象O) {
    修改条件使得上述等待方的条件成立;
    O.notify()或O.notifyAll();
}
复制代码

3. Pipeline input / output streams

● pipeline input / output streams use the data transfer between the main thread, the transmission medium is a memory.

● four methods of achieving pipeline input / output streams: PipedInputstream, PipedOutputStream, PipedWriter, PipedReader. For the first two bytes, the latter two character-oriented.

● input and output stream pipe flow when using need to be connected, that is, call connect () method, otherwise it will throw an IOException.

4. Thread.join () using

● When a thread A executed thread.join () (low-level calls wait () method), it represents a thread to thread A thread is finished and then wait () method returns the thread A from join to proceed.

● addition join () method, as well as join (long millis) and join (long millis, int nanos) includes two timeouts return characteristics.

● join () enables serial threads concurrently executing multiple execution.

● result of calling join () method is in a state of waiting threads can be interrupted by Thread.interrput () method.

Guess you like

Origin juejin.im/post/5ce8ea8ef265da1bc07e14f1