1. Reading and writing little scenes caused the problem?
Reading and writing are mutually exclusive in the process of writing a large number of read operations blocked and can not perform.
2. CopyOnWrite ideas to address (refer to the JDK CopyOnWriteArrayList source)
Raw data for a read operation, a copy of a copy of the original data for the write operation, a copy of the written assignment immediately to the original data, the use of the volatile keyword.
// This array is the core, as modified by the volatile // as long as the array of his latest assignment, other threads can immediately see the latest array Private transient volatile Object [] Array; public boolean the Add (E E) { Final = Lock of ReentrantLock the this .lock; Lock.lock (); the try { Object [] Elements = getArray (); int len = elements.length; // array copy of a copy out Object [] newElements = Arrays.copyOf (elements , . 1 + len ); // to modify the copy of the array, an element which is added the newElements [len] = E; //Copy of the array is then assigned to volatile variables setArray (the newElements); return to true ; } the finally { lock.unlock (); } } Final void setArray (Object [] A) { Array = A; }
3. kafka application, the data structure used in the memory buffer
private final ConcurrentMap<TopicPartition, Deque<RecordBatch>> batches = new CopyOnWriteMap<TopicPartition, Deque<RecordBatch>>();
// Typical volatile modification of the ordinary the Map Private volatile the Map <K, V> the Map; @Override public the synchronized V PUT (k K, V v) { // update the time to create a copy of a copy of the update, and then volatile variable assignment to write back to the Map <K, V> = Copy new new the HashMap <K, V> ( the this .map); V PREV = copy.put (K, V); the this .map = Collections.unmodifiableMap (Copy); return PREV; } @ override public V GET (Object K) { // when read directly read map data structure volatile variable references, no lock return as map.get (K); }
Reproduced source
Author: stone architecture cedar notes
link: https://juejin.im/post/5cd1724cf265da03a7440aae
Source: Nuggets
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
link: https://juejin.im/post/5cd1724cf265da03a7440aae
Source: Nuggets
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.