强撸ConcurrentHashMap

 

不知道是不是被逼无奈,还是突发感想,反正出自何种目的已无从考究,今天就要撸他,对,撸起来,起初的想法很简单,就是把ConcurrentHashMap这类从头到尾撸完

先从翻译开始

 * A hash table supporting full concurrency of retrievals and
 * high expected concurrency for updates. This class obeys the
 * same functional specification as {@link java.util.Hashtable}, and
 * includes versions of methods corresponding to each method of
 * {@code Hashtable}. However, even though all operations are
 * thread-safe, retrieval operations do not entail locking,
 * and there is not any support for locking the entire table
 * in a way that prevents all access.  This class is fully
 * interoperable with {@code Hashtable} in programs that rely on its
 * thread safety but not on its synchronization details.
一个哈希表支持高并发检索和超高预期的更新,这个类遵循着和【java.util.Hashtable】一样的方法准则,
并且包含了方法的版本和Hashtable一样的,然而,即所有的操作都是线程安全的,检索操作需要锁起来,
同时,也不支持一种堵塞所有访问的方式来锁住整个表,这个类和Hashtable在synchronization细节上不一样,
但是在编程上的操作和Hashtable一样,都依赖于线程安全。

Retrieval operations (including {@code get}) generally do not * block, so may overlap with update operations (including {@code put} * and {@code remove}). Retrievals reflect the results of the most * recently completed update operations holding upon their * onset. (More formally, an update operation for a given key bears a * happens-before relation with any (non-null) retrieval for * that key reporting the updated value.) For aggregate operations * such as {@code putAll} and {@code clear}, concurrent retrievals may * reflect insertion or removal of only some entries. Similarly, * Iterators, Spliterators and Enumerations return elements reflecting the * state of the hash table at some point at or since the creation of the * iterator/enumeration. They do not throw {@link * java.util.ConcurrentModificationException ConcurrentModificationException}. * However, iterators are designed to be used by only one thread at a time. * Bear in mind that the results of aggregate status methods including * {@code size}, {@code isEmpty}, and {@code containsValue} are typically * useful only when a map is not undergoing concurrent updates in other threads. * Otherwise the results of these methods reflect transient states * that may be adequate for monitoring or estimation purposes, but not * for program control. 查看操作(包括get)通常不会堵塞,所以更多情况是更新操作(包含put和remove操作), 查看能返回最近完成的鞥拿高薪操作结果,(正规点说就是一个对某key值的更新操作和对这个key的查询操作遵守happens-before规则) 一些组操作,比如putall,和clear,同时可能发生插入或者删除某些entries, 类似的,迭代,分裂还有枚举返回在建立hash表时的某一时刻的状态, 他们不会抛出java.util.ConcurrentModificationException ConcurrentModificationException异常, 但是,迭代器是单线程设计, 聚合方法有size isempty,containsValue在其他线程没有进行更新的时候才有用,否则这些方法只能查到当时的状态, 统计结果可以用来监测和估计,但不要用于程序控制

The table is dynamically expanded when there are too many * collisions (i.e., keys that have distinct hash codes but fall into * the same slot modulo the table size), with the expected average * effect of maintaining roughly two bins per mapping (corresponding * to a 0.75 load factor threshold for resizing). There may be much * variance around this average as mappings are added and removed, but * overall, this maintains a commonly accepted time/space tradeoff for * hash tables. However, resizing this or any other kind of hash * table may be a relatively slow operation. When possible, it is a * good idea to provide a size estimate as an optional {@code * initialCapacity} constructor argument. An additional optional * {@code loadFactor} constructor argument provides a further means of * customizing initial table capacity by specifying the table density * to be used in calculating the amount of space to allocate for the * given number of elements. Also, for compatibility with previous * versions of this class, constructors may optionally specify an * expected {@code concurrencyLevel} as an additional hint for * internal sizing. Note that using many keys with exactly the same * {@code hashCode()} is a sure way to slow down performance of any * hash table. To ameliorate impact, when keys are {@link Comparable}, * this class may use comparison order among keys to help break ties. 当有太多的碰撞的时候,表就会自动扩展(比如,key门有不同的hash值,但是进入同一个表大小的槽而失败) 维持一个平均值,大于每个mapping有2个bins(对应0.75负载系数的阈值),新增和删除操作时候, 可能方差更接近这个平均值 总而言之,保持了一个通常能接受的时间/空间,对于一个hash表来说 然后,调整它或者其他类型的hash表都会变成有点慢,如果看可能, 提供一个预算大小是一个好主意(@initialCapacity) 一个附加的选项改造参数提供了一个更多的含义去制定最初的表空间, 通过列出表的密集去使用在表的空间分给元素的数量,另外,也需要兼容之前的版本, 构建可能选择列出的预期,作为一个额外的点, 对于内部规定的尺寸来说, 注意,使用很多相同的键去降低hash表的编程,为了改善影响,当keys比较使用数据来帮助断点

* 

A {@link Set} projection of a ConcurrentHashMap may be created * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed * (using {@link #keySet(Object)} when only keys are of interest, and the * mapped values are (perhaps transiently) not used or all take the * same mapping value. *当仅仅只有keys,并且映射值没被使用或者所有的是同一映射值的时候, 一个concurrentHashMap项目可能被创建(详见链接newKeySet或newKeySet(int)),或者被查看(详见链接keyset), *

A ConcurrentHashMap can be used as scalable frequency map (a * form of histogram or multiset) by using {@link * java.util.concurrent.atomic.LongAdder} values and initializing via * {@link #computeIfAbsent computeIfAbsent}. For example, to add a count * to a {@code ConcurrentHashMap freqs}, you can use * {@code freqs.computeIfAbsent(k -> new LongAdder()).increment();} *Concurrenthashmap可以作为一个可检测的频率的map来使用(一个统计或者集合的标准)通过使用 详见链接java.util.concurrent.atomic.LongAdder}值和初始化经由 (详见链接link computeifabsent computeifabsent)例如,添加数字到一个 @code concurrenthashmapfreques,您可以使用 {@code freqs.computeIfAbsent(k -> new LongAdder()).increment();} *

This class and its views and iterators implement all of the * optional methods of the {@link Map} and {@link Iterator} * interfaces. *这个类和他的展示以及迭代元素,所有的可选择的方法(详见链接map)和接口(详见链接Iterator) *

Like {@link Hashtable} but unlike {@link HashMap}, this class * does not allow {@code null} to be used as a key or value. * 和hashtable类似,但是和hashmap不同,这个类不允许使用null作为key或者value *

ConcurrentHashMaps support a set of sequential and parallel bulk * operations that, unlike most {@link Stream} methods, are designed * to be safely, and often sensibly, applied even with maps that are * being concurrently updated by other threads; for example, when * computing a snapshot summary of the values in a shared registry. * There are three kinds of operation, each with four forms, accepting * functions with Keys, Values, Entries, and (Key, Value) arguments * and/or return values. Because the elements of a ConcurrentHashMap * are not ordered in any particular way, and may be processed in * different orders in different parallel executions, the correctness * of supplied functions should not depend on any ordering, or on any * other objects or values that may transiently change while * computation is in progress; and except for forEach actions, should * ideally be side-effect-free. Bulk operations on {@link java.util.Map.Entry} * objects do not support method {@code setValue}. * ConcurrentHashMaps支持一个有序的集合和一些了的桶操作,不想很多stream方法, 被设计成安全的和时常明显的,被其他县城更新,例如,当计算一个注册共享中的值 有三种操作方法,每种都有四种规则,来接受键/值/项和(键,值)参数 返回值 因为一个concurrentHashMap在特定的方法中不是有序的,并且可能不同的操作,在不同的并发执行中有不同的顺序 提供函数的准确性并不依赖于任何排序或者,任何其他的在操作过程中可以短暂变化的对象或值 除了forEach操作没有副作用,桶操作(详见链接java.util.Map.Entry),对象不支持方法setvalue

翻译好累啊,要吐了,接下来的有时间再翻译

你可能感兴趣的:(java相关知识)