First, the parallel world
Moore's law expires in the fall of 2004, Intel announced a total cancellation of the 4GHZ program, CPU to multi-core development, top computer scientist Donald, Knus, and the evaluation: in my opinion, this phenomenon (concurrency) is more or less due to the hardware designers have done nothing to cause, They take the responsibility for Moore's law invalidation to software developers.
1. Several concepts
Synchronous and asynchronous:
Concurrency and parallelism: both represent multiple tasks executing concurrently, but concurrency is biased toward multiple tasks alternating execution.
Critical section: data that is shared
Blocking and non-blocking:
Deadlock, hunger, and live Lock: deadlock for each other, hunger for a resource, a live lock to keep hitting
2. Concurrency level
Because of the existence of critical regions, concurrency between multithreading must be controlled. According to the control concurrency strategy, divided into blocking, no hunger, no barrier, no lock, no wait 5 kinds
3. Java language Related: JMM (Java memory model)
The key technology points in JMM are built around the atomicity, visibility and ordering of multithreading.
(1), atomicity: Non-disruptive operation
(2), Visibility: When a thread modifies the value of a shared variable, the other thread is immediately aware of the change.
(3), Order: When the program executes, the command can be rearranged to achieve the optimization effect, but in multi-threading, because of the interaction of threads, the reordering of threads will disrupt the execution of other threads.
Second, Java parallel 1, process and thread
A process is a program of a computer on a data set of a movement activity, the system is the basic unit of resource allocation. A process is the basic execution entity of a program, and a process is a container of threads.
2. State Transitions for threads
Thead class Method: Start () Open thread, run () thread execution entity, sleep () sleeping thread does not release resources, yield () let cpu,join () wait for thread to run end, suspend () hangs, resume () restarts
The object class method: Wait () releases the listener, the thread goes into a blocking state, notify () randomly wakes up one, Notifyall () wakes all. Wait,notify,notifyall to appear in the Synchronize synchronization block because of concurrency problems
3. Volatile
The volatile keyword tells the virtual confidential key not to change the optimization target instruction arbitrarily, the order of JMM. But there is no guarantee of atomic manipulation through volatile.
Third, Java and contract
0. Introduction to Concurrent Collections
Concurrenthashmap: Efficient concurrency HashMap, thread safety
Copyonwritearraylist: In the case of read and write less, this list performance is very good, far-ho-woo vector, are thread-safe
Concurrentlinkedqueue: Efficient concurrent queues with linked lists for thread safety
Blockingqueue: A blocking interface implemented inside a JDK through linked lists, arrays, etc.
Concurrentskiplistmap: The implementation of the jump table, which is a map, using the data structure of the jump table to quickly find
1, Blockingqueue
This interface is one of the 线程安全
存取实例
queues. Usage Scenarios
Blockingqueue is typically used for one thread to produce an object, while another thread consumes the scene of those objects.
2, HashMap, HashTable, concurrenthashmap contrast
HashMap thread is unsafe, hashtable,concurrenthashmap thread safe
Concurrenthashmap uses lock segmentation technology to be more efficient than Hashtable
Iv. parallel mode and algorithm 1, single case mode 2, invariant mode 3, producer Consumer 4, future mode
。。。。
Java High concurrency programming