39 |
|
* |
40 |
|
* <p> The allowed concurrency among update operations is guided by |
41 |
|
* the optional <tt>concurrencyLevel</tt> constructor argument |
42 |
< |
* (default 16), which is used as a hint for internal sizing. The |
42 |
> |
* (default {@value #DEFAULT_CONCURRENCY_LEVEL}), which is used as a hint for internal sizing. The |
43 |
|
* table is internally partitioned to try to permit the indicated |
44 |
|
* number of concurrent updates without contention. Because placement |
45 |
|
* in hash tables is essentially random, the actual concurrency will |
84 |
|
/* ---------------- Constants -------------- */ |
85 |
|
|
86 |
|
/** |
87 |
< |
* The default initial number of table slots for this table. |
88 |
< |
* Used when not otherwise specified in constructor. |
87 |
> |
* The default initial capacity for this table, |
88 |
> |
* used when not otherwise specified in a constructor. |
89 |
|
*/ |
90 |
< |
static int DEFAULT_INITIAL_CAPACITY = 16; |
90 |
> |
public static final int DEFAULT_INITIAL_CAPACITY = 16; |
91 |
> |
|
92 |
> |
/** |
93 |
> |
* The default load factor for this table, used when not |
94 |
> |
* otherwise specified in a constructor. |
95 |
> |
*/ |
96 |
> |
public static final float DEFAULT_LOAD_FACTOR = 0.75f; |
97 |
> |
|
98 |
> |
/** |
99 |
> |
* The default concurrency level for this table, used when not |
100 |
> |
* otherwise specified in a constructor. |
101 |
> |
**/ |
102 |
> |
public static final int DEFAULT_CONCURRENCY_LEVEL = 16; |
103 |
|
|
104 |
|
/** |
105 |
|
* The maximum capacity, used if a higher value is implicitly |
110 |
|
static final int MAXIMUM_CAPACITY = 1 << 30; |
111 |
|
|
112 |
|
/** |
101 |
– |
* The default load factor for this table. Used when not |
102 |
– |
* otherwise specified in constructor. |
103 |
– |
*/ |
104 |
– |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
105 |
– |
|
106 |
– |
/** |
107 |
– |
* The default number of concurrency control segments. |
108 |
– |
**/ |
109 |
– |
static final int DEFAULT_SEGMENTS = 16; |
110 |
– |
|
111 |
– |
/** |
113 |
|
* The maximum number of segments to allow; used to bound |
114 |
|
* constructor arguments. |
115 |
|
*/ |
558 |
|
|
559 |
|
/** |
560 |
|
* Creates a new, empty map with the specified initial |
561 |
< |
* capacity and the specified load factor. |
561 |
> |
* capacity, load factor and concurrency level. |
562 |
|
* |
563 |
|
* @param initialCapacity the initial capacity. The implementation |
564 |
|
* performs internal sizing to accommodate this many elements. |
565 |
|
* @param loadFactor the load factor threshold, used to control resizing. |
566 |
+ |
* Resizing may be performed when the average number of elements per |
567 |
+ |
* bin exceeds this threshold. |
568 |
|
* @param concurrencyLevel the estimated number of concurrently |
569 |
|
* updating threads. The implementation performs internal sizing |
570 |
|
* to try to accommodate this many threads. |
606 |
|
|
607 |
|
/** |
608 |
|
* Creates a new, empty map with the specified initial capacity |
609 |
< |
* and load factor and with the default concurrencyLevel (16). |
609 |
> |
* and load factor and with the default concurrencyLevel ({@value |
610 |
> |
* #DEFAULT_CONCURRENCY_LEVEL}). |
611 |
|
* |
612 |
|
* @param initialCapacity The implementation performs internal |
613 |
|
* sizing to accommodate this many elements. |
616 |
|
* elements is negative or the load factor is nonpositive |
617 |
|
*/ |
618 |
|
public ConcurrentHashMap(int initialCapacity, float loadFactor) { |
619 |
< |
this(initialCapacity, loadFactor, DEFAULT_SEGMENTS); |
619 |
> |
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL); |
620 |
|
} |
621 |
|
|
622 |
|
/** |
623 |
< |
* Creates a new, empty map with the specified initial |
624 |
< |
* capacity, and with default load factor (0.75f) and |
625 |
< |
* concurrencyLevel (16). |
623 |
> |
* Creates a new, empty map with the specified initial capacity, |
624 |
> |
* and with default load factor ({@value #DEFAULT_LOAD_FACTOR}) |
625 |
> |
* and concurrencyLevel ({@value #DEFAULT_CONCURRENCY_LEVEL}). |
626 |
|
* |
627 |
|
* @param initialCapacity The implementation performs internal |
628 |
|
* sizing to accommodate this many elements. |
630 |
|
* elements is negative. |
631 |
|
*/ |
632 |
|
public ConcurrentHashMap(int initialCapacity) { |
633 |
< |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
633 |
> |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
634 |
|
} |
635 |
|
|
636 |
|
/** |
637 |
< |
* Creates a new, empty map with a default initial capacity (16), |
638 |
< |
* load factor (0.75f), and concurrencyLevel (16). |
637 |
> |
* Creates a new, empty map with a default initial capacity |
638 |
> |
* ({@value #DEFAULT_INITIAL_CAPACITY}), load factor ({@value |
639 |
> |
* #DEFAULT_LOAD_FACTOR}), and concurrencyLevel ({@value |
640 |
> |
* #DEFAULT_CONCURRENCY_LEVEL}). |
641 |
|
*/ |
642 |
|
public ConcurrentHashMap() { |
643 |
< |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
643 |
> |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
|
* Creates a new map with the same mappings as the given map. The |
648 |
|
* map is created with a capacity of 1.5 times the number of |
649 |
< |
* mappings in the given map or 16 (whichever is greater), and a |
650 |
< |
* default load factor (0.75f) and concurrencyLevel(16). |
649 |
> |
* mappings in the given map or {@value #DEFAULT_INITIAL_CAPACITY} |
650 |
> |
* (whichever is greater), and a default load factor ({@value |
651 |
> |
* #DEFAULT_LOAD_FACTOR}) and concurrencyLevel({@value |
652 |
> |
* #DEFAULT_CONCURRENCY_LEVEL}). |
653 |
|
* @param t the map |
654 |
|
*/ |
655 |
|
public ConcurrentHashMap(Map<? extends K, ? extends V> t) { |
656 |
|
this(Math.max((int) (t.size() / DEFAULT_LOAD_FACTOR) + 1, |
657 |
< |
16), |
658 |
< |
DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
657 |
> |
DEFAULT_INITIAL_CAPACITY), |
658 |
> |
DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
659 |
|
putAll(t); |
660 |
|
} |
661 |
|
|
662 |
< |
// inherit Map javadoc |
662 |
> |
/** |
663 |
> |
* Returns <tt>true</tt> if this map contains no key-value mappings. |
664 |
> |
* |
665 |
> |
* @return <tt>true</tt> if this map contains no key-value mappings. |
666 |
> |
*/ |
667 |
|
public boolean isEmpty() { |
668 |
|
final Segment[] segments = this.segments; |
669 |
|
/* |
696 |
|
return true; |
697 |
|
} |
698 |
|
|
699 |
< |
// inherit Map javadoc |
699 |
> |
/** |
700 |
> |
* Returns the number of key-value mappings in this map. If the |
701 |
> |
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
702 |
> |
* <tt>Integer.MAX_VALUE</tt>. |
703 |
> |
* |
704 |
> |
* @return the number of key-value mappings in this map. |
705 |
> |
*/ |
706 |
|
public int size() { |
707 |
|
final Segment[] segments = this.segments; |
708 |
|
long sum = 0; |