提交 7075453d 编写于 作者: D dl

7118066: Warnings in java.util.concurrent package

Reviewed-by: chegar, dholmes
上级 8821938b
...@@ -131,8 +131,9 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...@@ -131,8 +131,9 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E>
/** /**
* Returns item at index i. * Returns item at index i.
*/ */
@SuppressWarnings("unchecked")
final E itemAt(int i) { final E itemAt(int i) {
return this.<E>cast(items[i]); return (E) items[i];
} }
/** /**
...@@ -162,7 +163,8 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...@@ -162,7 +163,8 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E>
*/ */
private E extract() { private E extract() {
final Object[] items = this.items; final Object[] items = this.items;
E x = this.<E>cast(items[takeIndex]); @SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
items[takeIndex] = null; items[takeIndex] = null;
takeIndex = inc(takeIndex); takeIndex = inc(takeIndex);
--count; --count;
...@@ -647,7 +649,9 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...@@ -647,7 +649,9 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E>
int n = 0; int n = 0;
int max = count; int max = count;
while (n < max) { while (n < max) {
c.add(this.<E>cast(items[i])); @SuppressWarnings("unchecked")
E x = (E) items[i];
c.add(x);
items[i] = null; items[i] = null;
i = inc(i); i = inc(i);
++n; ++n;
...@@ -684,7 +688,9 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E> ...@@ -684,7 +688,9 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E>
int n = 0; int n = 0;
int max = (maxElements < count) ? maxElements : count; int max = (maxElements < count) ? maxElements : count;
while (n < max) { while (n < max) {
c.add(this.<E>cast(items[i])); @SuppressWarnings("unchecked")
E x = (E) items[i];
c.add(x);
items[i] = null; items[i] = null;
i = inc(i); i = inc(i);
++n; ++n;
......
...@@ -37,9 +37,6 @@ package java.util.concurrent; ...@@ -37,9 +37,6 @@ package java.util.concurrent;
import java.util.concurrent.locks.*; import java.util.concurrent.locks.*;
import java.util.*; import java.util.*;
import java.io.Serializable; import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/** /**
* A hash table supporting full concurrency of retrievals and * A hash table supporting full concurrency of retrievals and
...@@ -228,7 +225,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -228,7 +225,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = HashEntry.class; Class<?> k = HashEntry.class;
nextOffset = UNSAFE.objectFieldOffset nextOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("next")); (k.getDeclaredField("next"));
} catch (Exception e) { } catch (Exception e) {
...@@ -433,7 +430,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -433,7 +430,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
int newCapacity = oldCapacity << 1; int newCapacity = oldCapacity << 1;
threshold = (int)(newCapacity * loadFactor); threshold = (int)(newCapacity * loadFactor);
HashEntry<K,V>[] newTable = HashEntry<K,V>[] newTable =
(HashEntry<K,V>[]) new HashEntry[newCapacity]; (HashEntry<K,V>[]) new HashEntry<?,?>[newCapacity];
int sizeMask = newCapacity - 1; int sizeMask = newCapacity - 1;
for (int i = 0; i < oldCapacity ; i++) { for (int i = 0; i < oldCapacity ; i++) {
HashEntry<K,V> e = oldTable[i]; HashEntry<K,V> e = oldTable[i];
...@@ -677,7 +674,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -677,7 +674,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
int cap = proto.table.length; int cap = proto.table.length;
float lf = proto.loadFactor; float lf = proto.loadFactor;
int threshold = (int)(cap * lf); int threshold = (int)(cap * lf);
HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry[cap]; HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry<?,?>[cap];
if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u))
== null) { // recheck == null) { // recheck
Segment<K,V> s = new Segment<K,V>(lf, threshold, tab); Segment<K,V> s = new Segment<K,V>(lf, threshold, tab);
...@@ -694,7 +691,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -694,7 +691,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
// Hash-based segment and entry accesses // Hash-based segment and entry accesses
/** /**
* Get the segment for the given hash * Gets the segment for the given hash code.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Segment<K,V> segmentForHash(int h) { private Segment<K,V> segmentForHash(int h) {
...@@ -703,7 +700,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -703,7 +700,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
} }
/** /**
* Gets the table entry for the given segment and hash * Gets the table entry for the given segment and hash code.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static final <K,V> HashEntry<K,V> entryForHash(Segment<K,V> seg, int h) { static final <K,V> HashEntry<K,V> entryForHash(Segment<K,V> seg, int h) {
...@@ -758,8 +755,8 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -758,8 +755,8 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
// create segments and segments[0] // create segments and segments[0]
Segment<K,V> s0 = Segment<K,V> s0 =
new Segment<K,V>(loadFactor, (int)(cap * loadFactor), new Segment<K,V>(loadFactor, (int)(cap * loadFactor),
(HashEntry<K,V>[])new HashEntry[cap]); (HashEntry<K,V>[])new HashEntry<?,?>[cap]);
Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize]; Segment<K,V>[] ss = (Segment<K,V>[])new Segment<?,?>[ssize];
UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0] UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]
this.segments = ss; this.segments = ss;
} }
...@@ -916,6 +913,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -916,6 +913,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
* *
* @throws NullPointerException if the specified key is null * @throws NullPointerException if the specified key is null
*/ */
@SuppressWarnings("unchecked")
public V get(Object key) { public V get(Object key) {
Segment<K,V> s; // manually integrate access methods to reduce overhead Segment<K,V> s; // manually integrate access methods to reduce overhead
HashEntry<K,V>[] tab; HashEntry<K,V>[] tab;
...@@ -1026,7 +1024,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -1026,7 +1024,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
* full compatibility with class {@link java.util.Hashtable}, * full compatibility with class {@link java.util.Hashtable},
* which supported this method prior to introduction of the * which supported this method prior to introduction of the
* Java Collections framework. * Java Collections framework.
*
* @param value a value to search for * @param value a value to search for
* @return <tt>true</tt> if and only if some key maps to the * @return <tt>true</tt> if and only if some key maps to the
* <tt>value</tt> argument in this table as * <tt>value</tt> argument in this table as
...@@ -1262,7 +1260,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -1262,7 +1260,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
} }
/** /**
* Set nextEntry to first node of next non-empty table * Sets nextEntry to first node of next non-empty table
* (in backwards order, to simplify checks). * (in backwards order, to simplify checks).
*/ */
final void advance() { final void advance() {
...@@ -1326,12 +1324,14 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -1326,12 +1324,14 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
final class WriteThroughEntry final class WriteThroughEntry
extends AbstractMap.SimpleEntry<K,V> extends AbstractMap.SimpleEntry<K,V>
{ {
static final long serialVersionUID = 7249069246763182397L;
WriteThroughEntry(K k, V v) { WriteThroughEntry(K k, V v) {
super(k,v); super(k,v);
} }
/** /**
* Set our entry's value and write through to the map. The * Sets our entry's value and writes through to the map. The
* value to return is somewhat arbitrary here. Since a * value to return is somewhat arbitrary here. Since a
* WriteThroughEntry does not necessarily track asynchronous * WriteThroughEntry does not necessarily track asynchronous
* changes, the most recent "previous" value could be * changes, the most recent "previous" value could be
...@@ -1427,15 +1427,16 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -1427,15 +1427,16 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
/* ---------------- Serialization Support -------------- */ /* ---------------- Serialization Support -------------- */
/** /**
* Save the state of the <tt>ConcurrentHashMap</tt> instance to a * Saves the state of the <tt>ConcurrentHashMap</tt> instance to a
* stream (i.e., serialize it). * stream (i.e., serializes it).
* @param s the stream * @param s the stream
* @serialData * @serialData
* the key (Object) and value (Object) * the key (Object) and value (Object)
* for each key-value mapping, followed by a null pair. * for each key-value mapping, followed by a null pair.
* The key-value mappings are emitted in no particular order. * The key-value mappings are emitted in no particular order.
*/ */
private void writeObject(java.io.ObjectOutputStream s) throws IOException { private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// force all segments for serialization compatibility // force all segments for serialization compatibility
for (int k = 0; k < segments.length; ++k) for (int k = 0; k < segments.length; ++k)
ensureSegment(k); ensureSegment(k);
...@@ -1463,13 +1464,13 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -1463,13 +1464,13 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
} }
/** /**
* Reconstitute the <tt>ConcurrentHashMap</tt> instance from a * Reconstitutes the <tt>ConcurrentHashMap</tt> instance from a
* stream (i.e., deserialize it). * stream (i.e., deserializes it).
* @param s the stream * @param s the stream
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s) private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException { throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); s.defaultReadObject();
// Re-initialize segments to be minimally sized, and let grow. // Re-initialize segments to be minimally sized, and let grow.
...@@ -1479,7 +1480,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -1479,7 +1480,7 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
Segment<K,V> seg = segments[k]; Segment<K,V> seg = segments[k];
if (seg != null) { if (seg != null) {
seg.threshold = (int)(cap * seg.loadFactor); seg.threshold = (int)(cap * seg.loadFactor);
seg.table = (HashEntry<K,V>[]) new HashEntry[cap]; seg.table = (HashEntry<K,V>[]) new HashEntry<?,?>[cap];
} }
} }
...@@ -1504,8 +1505,8 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> ...@@ -1504,8 +1505,8 @@ public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
int ss, ts; int ss, ts;
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class tc = HashEntry[].class; Class<?> tc = HashEntry[].class;
Class sc = Segment[].class; Class<?> sc = Segment[].class;
TBASE = UNSAFE.arrayBaseOffset(tc); TBASE = UNSAFE.arrayBaseOffset(tc);
SBASE = UNSAFE.arrayBaseOffset(sc); SBASE = UNSAFE.arrayBaseOffset(sc);
ts = UNSAFE.arrayIndexScale(tc); ts = UNSAFE.arrayIndexScale(tc);
......
...@@ -335,7 +335,7 @@ public class ConcurrentLinkedDeque<E> ...@@ -335,7 +335,7 @@ public class ConcurrentLinkedDeque<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = Node.class; Class<?> k = Node.class;
prevOffset = UNSAFE.objectFieldOffset prevOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("prev")); (k.getDeclaredField("prev"));
itemOffset = UNSAFE.objectFieldOffset itemOffset = UNSAFE.objectFieldOffset
...@@ -1457,7 +1457,7 @@ public class ConcurrentLinkedDeque<E> ...@@ -1457,7 +1457,7 @@ public class ConcurrentLinkedDeque<E>
NEXT_TERMINATOR.prev = NEXT_TERMINATOR; NEXT_TERMINATOR.prev = NEXT_TERMINATOR;
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = ConcurrentLinkedDeque.class; Class<?> k = ConcurrentLinkedDeque.class;
headOffset = UNSAFE.objectFieldOffset headOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("head")); (k.getDeclaredField("head"));
tailOffset = UNSAFE.objectFieldOffset tailOffset = UNSAFE.objectFieldOffset
......
...@@ -208,7 +208,7 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> ...@@ -208,7 +208,7 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = Node.class; Class<?> k = Node.class;
itemOffset = UNSAFE.objectFieldOffset itemOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("item")); (k.getDeclaredField("item"));
nextOffset = UNSAFE.objectFieldOffset nextOffset = UNSAFE.objectFieldOffset
...@@ -823,7 +823,7 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E> ...@@ -823,7 +823,7 @@ public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = ConcurrentLinkedQueue.class; Class<?> k = ConcurrentLinkedQueue.class;
headOffset = UNSAFE.objectFieldOffset headOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("head")); (k.getDeclaredField("head"));
tailOffset = UNSAFE.objectFieldOffset tailOffset = UNSAFE.objectFieldOffset
......
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
package java.util.concurrent; package java.util.concurrent;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.*;
/** /**
* A scalable concurrent {@link ConcurrentNavigableMap} implementation. * A scalable concurrent {@link ConcurrentNavigableMap} implementation.
...@@ -90,6 +89,7 @@ import java.util.concurrent.atomic.*; ...@@ -90,6 +89,7 @@ import java.util.concurrent.atomic.*;
* @param <V> the type of mapped values * @param <V> the type of mapped values
* @since 1.6 * @since 1.6
*/ */
@SuppressWarnings("unchecked")
public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
implements ConcurrentNavigableMap<K,V>, implements ConcurrentNavigableMap<K,V>,
Cloneable, Cloneable,
...@@ -352,11 +352,11 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -352,11 +352,11 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
private transient int randomSeed; private transient int randomSeed;
/** Lazily initialized key set */ /** Lazily initialized key set */
private transient KeySet keySet; private transient KeySet<K> keySet;
/** Lazily initialized entry set */ /** Lazily initialized entry set */
private transient EntrySet entrySet; private transient EntrySet<K,V> entrySet;
/** Lazily initialized values collection */ /** Lazily initialized values collection */
private transient Values values; private transient Values<V> values;
/** Lazily initialized descending key set */ /** Lazily initialized descending key set */
private transient ConcurrentNavigableMap<K,V> descendingMap; private transient ConcurrentNavigableMap<K,V> descendingMap;
...@@ -517,7 +517,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -517,7 +517,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = Node.class; Class<?> k = Node.class;
valueOffset = UNSAFE.objectFieldOffset valueOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("value")); (k.getDeclaredField("value"));
nextOffset = UNSAFE.objectFieldOffset nextOffset = UNSAFE.objectFieldOffset
...@@ -597,7 +597,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -597,7 +597,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = Index.class; Class<?> k = Index.class;
rightOffset = UNSAFE.objectFieldOffset rightOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("right")); (k.getDeclaredField("right"));
} catch (Exception e) { } catch (Exception e) {
...@@ -933,7 +933,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -933,7 +933,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
* direction. * direction.
*/ */
level = max + 1; level = max + 1;
Index<K,V>[] idxs = (Index<K,V>[])new Index[level+1]; Index<K,V>[] idxs = (Index<K,V>[])new Index<?,?>[level+1];
Index<K,V> idx = null; Index<K,V> idx = null;
for (int i = 1; i <= level; ++i) for (int i = 1; i <= level; ++i)
idxs[i] = idx = new Index<K,V>(z, idx, null); idxs[i] = idx = new Index<K,V>(z, idx, null);
...@@ -1436,16 +1436,16 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -1436,16 +1436,16 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
* @return a shallow copy of this map * @return a shallow copy of this map
*/ */
public ConcurrentSkipListMap<K,V> clone() { public ConcurrentSkipListMap<K,V> clone() {
ConcurrentSkipListMap<K,V> clone = null;
try { try {
clone = (ConcurrentSkipListMap<K,V>) super.clone(); @SuppressWarnings("unchecked")
ConcurrentSkipListMap<K,V> clone =
(ConcurrentSkipListMap<K,V>) super.clone();
clone.initialize();
clone.buildFromSorted(this);
return clone;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new InternalError(); throw new InternalError();
} }
clone.initialize();
clone.buildFromSorted(this);
return clone;
} }
/** /**
...@@ -1507,7 +1507,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -1507,7 +1507,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
/* ---------------- Serialization -------------- */ /* ---------------- Serialization -------------- */
/** /**
* Save the state of this map to a stream. * Saves the state of this map to a stream (that is, serializes it).
* *
* @serialData The key (Object) and value (Object) for each * @serialData The key (Object) and value (Object) for each
* key-value mapping represented by the map, followed by * key-value mapping represented by the map, followed by
...@@ -1532,7 +1532,9 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -1532,7 +1532,9 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
} }
/** /**
* Reconstitute the map from a stream. * Reconstitutes the map from a stream (that is, deserializes it).
*
* @param s the stream
*/ */
private void readObject(final java.io.ObjectInputStream s) private void readObject(final java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException { throws java.io.IOException, ClassNotFoundException {
...@@ -1755,13 +1757,13 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -1755,13 +1757,13 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
* @return a navigable set view of the keys in this map * @return a navigable set view of the keys in this map
*/ */
public NavigableSet<K> keySet() { public NavigableSet<K> keySet() {
KeySet ks = keySet; KeySet<K> ks = keySet;
return (ks != null) ? ks : (keySet = new KeySet(this)); return (ks != null) ? ks : (keySet = new KeySet<K>(this));
} }
public NavigableSet<K> navigableKeySet() { public NavigableSet<K> navigableKeySet() {
KeySet ks = keySet; KeySet<K> ks = keySet;
return (ks != null) ? ks : (keySet = new KeySet(this)); return (ks != null) ? ks : (keySet = new KeySet<K>(this));
} }
/** /**
...@@ -1783,8 +1785,8 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -1783,8 +1785,8 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
* reflect any modifications subsequent to construction. * reflect any modifications subsequent to construction.
*/ */
public Collection<V> values() { public Collection<V> values() {
Values vs = values; Values<V> vs = values;
return (vs != null) ? vs : (values = new Values(this)); return (vs != null) ? vs : (values = new Values<V>(this));
} }
/** /**
...@@ -1812,8 +1814,8 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -1812,8 +1814,8 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
* sorted in ascending key order * sorted in ascending key order
*/ */
public Set<Map.Entry<K,V>> entrySet() { public Set<Map.Entry<K,V>> entrySet() {
EntrySet es = entrySet; EntrySet<K,V> es = entrySet;
return (es != null) ? es : (entrySet = new EntrySet(this)); return (es != null) ? es : (entrySet = new EntrySet<K,V>(this));
} }
public ConcurrentNavigableMap<K,V> descendingMap() { public ConcurrentNavigableMap<K,V> descendingMap() {
...@@ -2304,8 +2306,8 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2304,8 +2306,8 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
static final class KeySet<E> static final class KeySet<E>
extends AbstractSet<E> implements NavigableSet<E> { extends AbstractSet<E> implements NavigableSet<E> {
private final ConcurrentNavigableMap<E,Object> m; private final ConcurrentNavigableMap<E,?> m;
KeySet(ConcurrentNavigableMap<E,Object> map) { m = map; } KeySet(ConcurrentNavigableMap<E,?> map) { m = map; }
public int size() { return m.size(); } public int size() { return m.size(); }
public boolean isEmpty() { return m.isEmpty(); } public boolean isEmpty() { return m.isEmpty(); }
public boolean contains(Object o) { return m.containsKey(o); } public boolean contains(Object o) { return m.containsKey(o); }
...@@ -2319,11 +2321,11 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2319,11 +2321,11 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
public E first() { return m.firstKey(); } public E first() { return m.firstKey(); }
public E last() { return m.lastKey(); } public E last() { return m.lastKey(); }
public E pollFirst() { public E pollFirst() {
Map.Entry<E,Object> e = m.pollFirstEntry(); Map.Entry<E,?> e = m.pollFirstEntry();
return (e == null) ? null : e.getKey(); return (e == null) ? null : e.getKey();
} }
public E pollLast() { public E pollLast() {
Map.Entry<E,Object> e = m.pollLastEntry(); Map.Entry<E,?> e = m.pollLastEntry();
return (e == null) ? null : e.getKey(); return (e == null) ? null : e.getKey();
} }
public Iterator<E> iterator() { public Iterator<E> iterator() {
...@@ -2374,20 +2376,20 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2374,20 +2376,20 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
return tailSet(fromElement, true); return tailSet(fromElement, true);
} }
public NavigableSet<E> descendingSet() { public NavigableSet<E> descendingSet() {
return new KeySet(m.descendingMap()); return new KeySet<E>(m.descendingMap());
} }
} }
static final class Values<E> extends AbstractCollection<E> { static final class Values<E> extends AbstractCollection<E> {
private final ConcurrentNavigableMap<Object, E> m; private final ConcurrentNavigableMap<?, E> m;
Values(ConcurrentNavigableMap<Object, E> map) { Values(ConcurrentNavigableMap<?, E> map) {
m = map; m = map;
} }
public Iterator<E> iterator() { public Iterator<E> iterator() {
if (m instanceof ConcurrentSkipListMap) if (m instanceof ConcurrentSkipListMap)
return ((ConcurrentSkipListMap<Object,E>)m).valueIterator(); return ((ConcurrentSkipListMap<?,E>)m).valueIterator();
else else
return ((SubMap<Object,E>)m).valueIterator(); return ((SubMap<?,E>)m).valueIterator();
} }
public boolean isEmpty() { public boolean isEmpty() {
return m.isEmpty(); return m.isEmpty();
...@@ -2421,14 +2423,14 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2421,14 +2423,14 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
public boolean contains(Object o) { public boolean contains(Object o) {
if (!(o instanceof Map.Entry)) if (!(o instanceof Map.Entry))
return false; return false;
Map.Entry<K1,V1> e = (Map.Entry<K1,V1>)o; Map.Entry<?,?> e = (Map.Entry<?,?>)o;
V1 v = m.get(e.getKey()); V1 v = m.get(e.getKey());
return v != null && v.equals(e.getValue()); return v != null && v.equals(e.getValue());
} }
public boolean remove(Object o) { public boolean remove(Object o) {
if (!(o instanceof Map.Entry)) if (!(o instanceof Map.Entry))
return false; return false;
Map.Entry<K1,V1> e = (Map.Entry<K1,V1>)o; Map.Entry<?,?> e = (Map.Entry<?,?>)o;
return m.remove(e.getKey(), return m.remove(e.getKey(),
e.getValue()); e.getValue());
} }
...@@ -2568,9 +2570,9 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2568,9 +2570,9 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
if (lo == null) if (lo == null)
return m.findFirst(); return m.findFirst();
else if (loInclusive) else if (loInclusive)
return m.findNear(lo, m.GT|m.EQ); return m.findNear(lo, GT|EQ);
else else
return m.findNear(lo, m.GT); return m.findNear(lo, GT);
} }
/** /**
...@@ -2581,9 +2583,9 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2581,9 +2583,9 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
if (hi == null) if (hi == null)
return m.findLast(); return m.findLast();
else if (hiInclusive) else if (hiInclusive)
return m.findNear(hi, m.LT|m.EQ); return m.findNear(hi, LT|EQ);
else else
return m.findNear(hi, m.LT); return m.findNear(hi, LT);
} }
/** /**
...@@ -2665,15 +2667,15 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2665,15 +2667,15 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
*/ */
private Map.Entry<K,V> getNearEntry(K key, int rel) { private Map.Entry<K,V> getNearEntry(K key, int rel) {
if (isDescending) { // adjust relation for direction if (isDescending) { // adjust relation for direction
if ((rel & m.LT) == 0) if ((rel & LT) == 0)
rel |= m.LT; rel |= LT;
else else
rel &= ~m.LT; rel &= ~LT;
} }
if (tooLow(key)) if (tooLow(key))
return ((rel & m.LT) != 0) ? null : lowestEntry(); return ((rel & LT) != 0) ? null : lowestEntry();
if (tooHigh(key)) if (tooHigh(key))
return ((rel & m.LT) != 0) ? highestEntry() : null; return ((rel & LT) != 0) ? highestEntry() : null;
for (;;) { for (;;) {
Node<K,V> n = m.findNear(key, rel); Node<K,V> n = m.findNear(key, rel);
if (n == null || !inBounds(n.key)) if (n == null || !inBounds(n.key))
...@@ -2688,13 +2690,13 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2688,13 +2690,13 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
// Almost the same as getNearEntry, except for keys // Almost the same as getNearEntry, except for keys
private K getNearKey(K key, int rel) { private K getNearKey(K key, int rel) {
if (isDescending) { // adjust relation for direction if (isDescending) { // adjust relation for direction
if ((rel & m.LT) == 0) if ((rel & LT) == 0)
rel |= m.LT; rel |= LT;
else else
rel &= ~m.LT; rel &= ~LT;
} }
if (tooLow(key)) { if (tooLow(key)) {
if ((rel & m.LT) == 0) { if ((rel & LT) == 0) {
ConcurrentSkipListMap.Node<K,V> n = loNode(); ConcurrentSkipListMap.Node<K,V> n = loNode();
if (isBeforeEnd(n)) if (isBeforeEnd(n))
return n.key; return n.key;
...@@ -2702,7 +2704,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2702,7 +2704,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
return null; return null;
} }
if (tooHigh(key)) { if (tooHigh(key)) {
if ((rel & m.LT) != 0) { if ((rel & LT) != 0) {
ConcurrentSkipListMap.Node<K,V> n = hiNode(); ConcurrentSkipListMap.Node<K,V> n = hiNode();
if (n != null) { if (n != null) {
K last = n.key; K last = n.key;
...@@ -2734,7 +2736,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2734,7 +2736,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
public V get(Object key) { public V get(Object key) {
if (key == null) throw new NullPointerException(); if (key == null) throw new NullPointerException();
K k = (K)key; K k = (K)key;
return ((!inBounds(k)) ? null : m.get(k)); return (!inBounds(k)) ? null : m.get(k);
} }
public V put(K key, V value) { public V put(K key, V value) {
...@@ -2901,35 +2903,35 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2901,35 +2903,35 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
/* ---------------- Relational methods -------------- */ /* ---------------- Relational methods -------------- */
public Map.Entry<K,V> ceilingEntry(K key) { public Map.Entry<K,V> ceilingEntry(K key) {
return getNearEntry(key, (m.GT|m.EQ)); return getNearEntry(key, GT|EQ);
} }
public K ceilingKey(K key) { public K ceilingKey(K key) {
return getNearKey(key, (m.GT|m.EQ)); return getNearKey(key, GT|EQ);
} }
public Map.Entry<K,V> lowerEntry(K key) { public Map.Entry<K,V> lowerEntry(K key) {
return getNearEntry(key, (m.LT)); return getNearEntry(key, LT);
} }
public K lowerKey(K key) { public K lowerKey(K key) {
return getNearKey(key, (m.LT)); return getNearKey(key, LT);
} }
public Map.Entry<K,V> floorEntry(K key) { public Map.Entry<K,V> floorEntry(K key) {
return getNearEntry(key, (m.LT|m.EQ)); return getNearEntry(key, LT|EQ);
} }
public K floorKey(K key) { public K floorKey(K key) {
return getNearKey(key, (m.LT|m.EQ)); return getNearKey(key, LT|EQ);
} }
public Map.Entry<K,V> higherEntry(K key) { public Map.Entry<K,V> higherEntry(K key) {
return getNearEntry(key, (m.GT)); return getNearEntry(key, GT);
} }
public K higherKey(K key) { public K higherKey(K key) {
return getNearKey(key, (m.GT)); return getNearKey(key, GT);
} }
public K firstKey() { public K firstKey() {
...@@ -2960,22 +2962,22 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -2960,22 +2962,22 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
public NavigableSet<K> keySet() { public NavigableSet<K> keySet() {
KeySet<K> ks = keySetView; KeySet<K> ks = keySetView;
return (ks != null) ? ks : (keySetView = new KeySet(this)); return (ks != null) ? ks : (keySetView = new KeySet<K>(this));
} }
public NavigableSet<K> navigableKeySet() { public NavigableSet<K> navigableKeySet() {
KeySet<K> ks = keySetView; KeySet<K> ks = keySetView;
return (ks != null) ? ks : (keySetView = new KeySet(this)); return (ks != null) ? ks : (keySetView = new KeySet<K>(this));
} }
public Collection<V> values() { public Collection<V> values() {
Collection<V> vs = valuesView; Collection<V> vs = valuesView;
return (vs != null) ? vs : (valuesView = new Values(this)); return (vs != null) ? vs : (valuesView = new Values<V>(this));
} }
public Set<Map.Entry<K,V>> entrySet() { public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es = entrySetView; Set<Map.Entry<K,V>> es = entrySetView;
return (es != null) ? es : (entrySetView = new EntrySet(this)); return (es != null) ? es : (entrySetView = new EntrySet<K,V>(this));
} }
public NavigableSet<K> descendingKeySet() { public NavigableSet<K> descendingKeySet() {
...@@ -3109,7 +3111,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> ...@@ -3109,7 +3111,7 @@ public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = ConcurrentSkipListMap.class; Class<?> k = ConcurrentSkipListMap.class;
headOffset = UNSAFE.objectFieldOffset headOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("head")); (k.getDeclaredField("head"));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
package java.util.concurrent; package java.util.concurrent;
import java.util.*; import java.util.*;
import sun.misc.Unsafe;
/** /**
* A scalable concurrent {@link NavigableSet} implementation based on * A scalable concurrent {@link NavigableSet} implementation based on
...@@ -158,15 +157,15 @@ public class ConcurrentSkipListSet<E> ...@@ -158,15 +157,15 @@ public class ConcurrentSkipListSet<E>
* @return a shallow copy of this set * @return a shallow copy of this set
*/ */
public ConcurrentSkipListSet<E> clone() { public ConcurrentSkipListSet<E> clone() {
ConcurrentSkipListSet<E> clone = null;
try { try {
clone = (ConcurrentSkipListSet<E>) super.clone(); @SuppressWarnings("unchecked")
clone.setMap(new ConcurrentSkipListMap(m)); ConcurrentSkipListSet<E> clone =
(ConcurrentSkipListSet<E>) super.clone();
clone.setMap(new ConcurrentSkipListMap<E,Object>(m));
return clone;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new InternalError(); throw new InternalError();
} }
return clone;
} }
/* ---------------- Set operations -------------- */ /* ---------------- Set operations -------------- */
...@@ -322,8 +321,8 @@ public class ConcurrentSkipListSet<E> ...@@ -322,8 +321,8 @@ public class ConcurrentSkipListSet<E>
public boolean removeAll(Collection<?> c) { public boolean removeAll(Collection<?> c) {
// Override AbstractSet version to avoid unnecessary call to size() // Override AbstractSet version to avoid unnecessary call to size()
boolean modified = false; boolean modified = false;
for (Iterator<?> i = c.iterator(); i.hasNext(); ) for (Object e : c)
if (remove(i.next())) if (remove(e))
modified = true; modified = true;
return modified; return modified;
} }
...@@ -468,7 +467,7 @@ public class ConcurrentSkipListSet<E> ...@@ -468,7 +467,7 @@ public class ConcurrentSkipListSet<E>
* @return a reverse order view of this set * @return a reverse order view of this set
*/ */
public NavigableSet<E> descendingSet() { public NavigableSet<E> descendingSet() {
return new ConcurrentSkipListSet(m.descendingMap()); return new ConcurrentSkipListSet<E>(m.descendingMap());
} }
// Support for resetting map in clone // Support for resetting map in clone
...@@ -481,7 +480,7 @@ public class ConcurrentSkipListSet<E> ...@@ -481,7 +480,7 @@ public class ConcurrentSkipListSet<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = ConcurrentSkipListSet.class; Class<?> k = ConcurrentSkipListSet.class;
mapOffset = UNSAFE.objectFieldOffset mapOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("m")); (k.getDeclaredField("m"));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -36,7 +36,6 @@ ...@@ -36,7 +36,6 @@
package java.util.concurrent; package java.util.concurrent;
import java.util.*; import java.util.*;
import java.util.concurrent.locks.*; import java.util.concurrent.locks.*;
import sun.misc.Unsafe;
/** /**
* A thread-safe variant of {@link java.util.ArrayList} in which all mutative * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
...@@ -281,9 +280,11 @@ public class CopyOnWriteArrayList<E> ...@@ -281,9 +280,11 @@ public class CopyOnWriteArrayList<E>
*/ */
public Object clone() { public Object clone() {
try { try {
CopyOnWriteArrayList c = (CopyOnWriteArrayList)(super.clone()); @SuppressWarnings("unchecked")
c.resetLock(); CopyOnWriteArrayList<E> clone =
return c; (CopyOnWriteArrayList<E>) super.clone();
clone.resetLock();
return clone;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable // this shouldn't happen, since we are Cloneable
throw new InternalError(); throw new InternalError();
...@@ -1330,7 +1331,7 @@ public class CopyOnWriteArrayList<E> ...@@ -1330,7 +1331,7 @@ public class CopyOnWriteArrayList<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = CopyOnWriteArrayList.class; Class<?> k = CopyOnWriteArrayList.class;
lockOffset = UNSAFE.objectFieldOffset lockOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("lock")); (k.getDeclaredField("lock"));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -531,7 +531,7 @@ public class DelayQueue<E extends Delayed> extends AbstractQueue<E> ...@@ -531,7 +531,7 @@ public class DelayQueue<E extends Delayed> extends AbstractQueue<E>
// not just a .equals element. // not just a .equals element.
lock.lock(); lock.lock();
try { try {
for (Iterator it = q.iterator(); it.hasNext(); ) { for (Iterator<E> it = q.iterator(); it.hasNext(); ) {
if (it.next() == x) { if (it.next() == x) {
it.remove(); it.remove();
return; return;
......
...@@ -279,6 +279,7 @@ public class Exchanger<V> { ...@@ -279,6 +279,7 @@ public class Exchanger<V> {
* into hole. This class cannot be parameterized as "V" because * into hole. This class cannot be parameterized as "V" because
* of the use of non-V CANCEL sentinels. * of the use of non-V CANCEL sentinels.
*/ */
@SuppressWarnings("serial")
private static final class Node extends AtomicReference<Object> { private static final class Node extends AtomicReference<Object> {
/** The element offered by the Thread creating this node. */ /** The element offered by the Thread creating this node. */
public final Object item; public final Object item;
...@@ -303,6 +304,7 @@ public class Exchanger<V> { ...@@ -303,6 +304,7 @@ public class Exchanger<V> {
* would improve throughput more than enough to outweigh using * would improve throughput more than enough to outweigh using
* extra space. * extra space.
*/ */
@SuppressWarnings("serial")
private static final class Slot extends AtomicReference<Object> { private static final class Slot extends AtomicReference<Object> {
// Improve likelihood of isolation on <= 64 byte cache lines // Improve likelihood of isolation on <= 64 byte cache lines
long q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, qa, qb, qc, qd, qe; long q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, qa, qb, qc, qd, qe;
...@@ -616,13 +618,14 @@ public class Exchanger<V> { ...@@ -616,13 +618,14 @@ public class Exchanger<V> {
* @throws InterruptedException if the current thread was * @throws InterruptedException if the current thread was
* interrupted while waiting * interrupted while waiting
*/ */
@SuppressWarnings("unchecked")
public V exchange(V x) throws InterruptedException { public V exchange(V x) throws InterruptedException {
if (!Thread.interrupted()) { if (!Thread.interrupted()) {
Object v = doExchange((x == null) ? NULL_ITEM : x, false, 0); Object o = doExchange((x == null) ? NULL_ITEM : x, false, 0);
if (v == NULL_ITEM) if (o == NULL_ITEM)
return null; return null;
if (v != CANCEL) if (o != CANCEL)
return (V)v; return (V)o;
Thread.interrupted(); // Clear interrupt status on IE throw Thread.interrupted(); // Clear interrupt status on IE throw
} }
throw new InterruptedException(); throw new InterruptedException();
...@@ -670,15 +673,16 @@ public class Exchanger<V> { ...@@ -670,15 +673,16 @@ public class Exchanger<V> {
* @throws TimeoutException if the specified waiting time elapses * @throws TimeoutException if the specified waiting time elapses
* before another thread enters the exchange * before another thread enters the exchange
*/ */
@SuppressWarnings("unchecked")
public V exchange(V x, long timeout, TimeUnit unit) public V exchange(V x, long timeout, TimeUnit unit)
throws InterruptedException, TimeoutException { throws InterruptedException, TimeoutException {
if (!Thread.interrupted()) { if (!Thread.interrupted()) {
Object v = doExchange((x == null) ? NULL_ITEM : x, Object o = doExchange((x == null) ? NULL_ITEM : x,
true, unit.toNanos(timeout)); true, unit.toNanos(timeout));
if (v == NULL_ITEM) if (o == NULL_ITEM)
return null; return null;
if (v != CANCEL) if (o != CANCEL)
return (V)v; return (V)o;
if (!Thread.interrupted()) if (!Thread.interrupted())
throw new TimeoutException(); throw new TimeoutException();
} }
......
...@@ -2150,7 +2150,7 @@ public class ForkJoinPool extends AbstractExecutorService { ...@@ -2150,7 +2150,7 @@ public class ForkJoinPool extends AbstractExecutorService {
int s; int s;
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = ForkJoinPool.class; Class<?> k = ForkJoinPool.class;
ctlOffset = UNSAFE.objectFieldOffset ctlOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("ctl")); (k.getDeclaredField("ctl"));
stealCountOffset = UNSAFE.objectFieldOffset stealCountOffset = UNSAFE.objectFieldOffset
...@@ -2163,7 +2163,7 @@ public class ForkJoinPool extends AbstractExecutorService { ...@@ -2163,7 +2163,7 @@ public class ForkJoinPool extends AbstractExecutorService {
(k.getDeclaredField("scanGuard")); (k.getDeclaredField("scanGuard"));
nextWorkerNumberOffset = UNSAFE.objectFieldOffset nextWorkerNumberOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("nextWorkerNumber")); (k.getDeclaredField("nextWorkerNumber"));
Class a = ForkJoinTask[].class; Class<?> a = ForkJoinTask[].class;
ABASE = UNSAFE.arrayBaseOffset(a); ABASE = UNSAFE.arrayBaseOffset(a);
s = UNSAFE.arrayIndexScale(a); s = UNSAFE.arrayIndexScale(a);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -520,7 +520,7 @@ public abstract class ForkJoinTask<V> implements Future<V>, Serializable { ...@@ -520,7 +520,7 @@ public abstract class ForkJoinTask<V> implements Future<V>, Serializable {
if (e == null || (ex = e.ex) == null) if (e == null || (ex = e.ex) == null)
return null; return null;
if (e.thrower != Thread.currentThread().getId()) { if (e.thrower != Thread.currentThread().getId()) {
Class ec = ex.getClass(); Class<? extends Throwable> ec = ex.getClass();
try { try {
Constructor<?> noArgCtor = null; Constructor<?> noArgCtor = null;
Constructor<?>[] cs = ec.getConstructors();// public ctors only Constructor<?>[] cs = ec.getConstructors();// public ctors only
......
...@@ -192,7 +192,7 @@ public class ForkJoinWorkerThread extends Thread { ...@@ -192,7 +192,7 @@ public class ForkJoinWorkerThread extends Thread {
/** /**
* The work-stealing queue array. Size must be a power of two. * The work-stealing queue array. Size must be a power of two.
* Initialized when started (as oposed to when constructed), to * Initialized when started (as opposed to when constructed), to
* improve memory locality. * improve memory locality.
*/ */
ForkJoinTask<?>[] queue; ForkJoinTask<?>[] queue;
...@@ -360,7 +360,7 @@ public class ForkJoinWorkerThread extends Thread { ...@@ -360,7 +360,7 @@ public class ForkJoinWorkerThread extends Thread {
*/ */
protected void onStart() { protected void onStart() {
queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
int r = pool.workerSeedGenerator.nextInt(); int r = ForkJoinPool.workerSeedGenerator.nextInt();
seed = (r == 0) ? 1 : r; // must be nonzero seed = (r == 0) ? 1 : r; // must be nonzero
} }
...@@ -846,7 +846,7 @@ public class ForkJoinWorkerThread extends Thread { ...@@ -846,7 +846,7 @@ public class ForkJoinWorkerThread extends Thread {
(b = v.queueBase) != v.queueTop && (b = v.queueBase) != v.queueTop &&
(q = v.queue) != null && (q = v.queue) != null &&
(i = (q.length - 1) & b) >= 0 && (i = (q.length - 1) & b) >= 0 &&
q[i] == t) { q[i] == t) {
long u = (i << ASHIFT) + ABASE; long u = (i << ASHIFT) + ABASE;
if (v.queueBase == b && if (v.queueBase == b &&
UNSAFE.compareAndSwapObject(q, u, t, null)) { UNSAFE.compareAndSwapObject(q, u, t, null)) {
...@@ -984,7 +984,7 @@ public class ForkJoinWorkerThread extends Thread { ...@@ -984,7 +984,7 @@ public class ForkJoinWorkerThread extends Thread {
int s; int s;
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class a = ForkJoinTask[].class; Class<?> a = ForkJoinTask[].class;
ABASE = UNSAFE.arrayBaseOffset(a); ABASE = UNSAFE.arrayBaseOffset(a);
s = UNSAFE.arrayIndexScale(a); s = UNSAFE.arrayIndexScale(a);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -330,8 +330,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -330,8 +330,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* of less-contended queues. During spins threads check their * of less-contended queues. During spins threads check their
* interrupt status and generate a thread-local random number * interrupt status and generate a thread-local random number
* to decide to occasionally perform a Thread.yield. While * to decide to occasionally perform a Thread.yield. While
* yield has underdefined specs, we assume that might it help, * yield has underdefined specs, we assume that it might help,
* and will not hurt in limiting impact of spinning on busy * and will not hurt, in limiting impact of spinning on busy
* systems. We also use smaller (1/2) spins for nodes that are * systems. We also use smaller (1/2) spins for nodes that are
* not known to be front but whose predecessors have not * not known to be front but whose predecessors have not
* blocked -- these "chained" spins avoid artifacts of * blocked -- these "chained" spins avoid artifacts of
...@@ -542,7 +542,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -542,7 +542,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = Node.class; Class<?> k = Node.class;
itemOffset = UNSAFE.objectFieldOffset itemOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("item")); (k.getDeclaredField("item"));
nextOffset = UNSAFE.objectFieldOffset nextOffset = UNSAFE.objectFieldOffset
...@@ -627,7 +627,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -627,7 +627,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
break; // unless slack < 2 break; // unless slack < 2
} }
LockSupport.unpark(p.waiter); LockSupport.unpark(p.waiter);
return this.<E>cast(item); return LinkedTransferQueue.<E>cast(item);
} }
} }
Node n = p.next; Node n = p.next;
...@@ -705,7 +705,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -705,7 +705,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
if (item != e) { // matched if (item != e) { // matched
// assert item != s; // assert item != s;
s.forgetContents(); // avoid garbage s.forgetContents(); // avoid garbage
return this.<E>cast(item); return LinkedTransferQueue.<E>cast(item);
} }
if ((w.isInterrupted() || (timed && nanos <= 0)) && if ((w.isInterrupted() || (timed && nanos <= 0)) &&
s.casItem(e, s)) { // cancel s.casItem(e, s)) { // cancel
...@@ -786,7 +786,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -786,7 +786,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
Object item = p.item; Object item = p.item;
if (p.isData) { if (p.isData) {
if (item != null && item != p) if (item != null && item != p)
return this.<E>cast(item); return LinkedTransferQueue.<E>cast(item);
} }
else if (item == null) else if (item == null)
return null; return null;
...@@ -1008,7 +1008,6 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -1008,7 +1008,6 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
return false; return false;
} }
/** /**
* Creates an initially empty {@code LinkedTransferQueue}. * Creates an initially empty {@code LinkedTransferQueue}.
*/ */
...@@ -1045,7 +1044,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -1045,7 +1044,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* return {@code false}. * return {@code false}.
* *
* @return {@code true} (as specified by * @return {@code true} (as specified by
* {@link BlockingQueue#offer(Object,long,TimeUnit) BlockingQueue.offer}) * {@link java.util.concurrent.BlockingQueue#offer(Object,long,TimeUnit)
* BlockingQueue.offer})
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
*/ */
public boolean offer(E e, long timeout, TimeUnit unit) { public boolean offer(E e, long timeout, TimeUnit unit) {
...@@ -1162,8 +1162,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -1162,8 +1162,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
if (c == this) if (c == this)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
int n = 0; int n = 0;
E e; for (E e; (e = poll()) != null;) {
while ( (e = poll()) != null) {
c.add(e); c.add(e);
++n; ++n;
} }
...@@ -1180,8 +1179,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -1180,8 +1179,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
if (c == this) if (c == this)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
int n = 0; int n = 0;
E e; for (E e; n < maxElements && (e = poll()) != null;) {
while (n < maxElements && (e = poll()) != null) {
c.add(e); c.add(e);
++n; ++n;
} }
...@@ -1288,7 +1286,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -1288,7 +1286,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* {@code LinkedTransferQueue} is not capacity constrained. * {@code LinkedTransferQueue} is not capacity constrained.
* *
* @return {@code Integer.MAX_VALUE} (as specified by * @return {@code Integer.MAX_VALUE} (as specified by
* {@link BlockingQueue#remainingCapacity()}) * {@link java.util.concurrent.BlockingQueue#remainingCapacity()
* BlockingQueue.remainingCapacity})
*/ */
public int remainingCapacity() { public int remainingCapacity() {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
...@@ -1320,7 +1319,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -1320,7 +1319,8 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
throws java.io.IOException, ClassNotFoundException { throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); s.defaultReadObject();
for (;;) { for (;;) {
@SuppressWarnings("unchecked") E item = (E) s.readObject(); @SuppressWarnings("unchecked")
E item = (E) s.readObject();
if (item == null) if (item == null)
break; break;
else else
...@@ -1337,7 +1337,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E> ...@@ -1337,7 +1337,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = LinkedTransferQueue.class; Class<?> k = LinkedTransferQueue.class;
headOffset = UNSAFE.objectFieldOffset headOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("head")); (k.getDeclaredField("head"));
tailOffset = UNSAFE.objectFieldOffset tailOffset = UNSAFE.objectFieldOffset
......
...@@ -1142,7 +1142,7 @@ public class Phaser { ...@@ -1142,7 +1142,7 @@ public class Phaser {
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = Phaser.class; Class<?> k = Phaser.class;
stateOffset = UNSAFE.objectFieldOffset stateOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("state")); (k.getDeclaredField("state"));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -94,6 +94,7 @@ import java.util.*; ...@@ -94,6 +94,7 @@ import java.util.*;
* @author Doug Lea * @author Doug Lea
* @param <E> the type of elements held in this collection * @param <E> the type of elements held in this collection
*/ */
@SuppressWarnings("unchecked")
public class PriorityBlockingQueue<E> extends AbstractQueue<E> public class PriorityBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable { implements BlockingQueue<E>, java.io.Serializable {
private static final long serialVersionUID = 5595510919245408276L; private static final long serialVersionUID = 5595510919245408276L;
...@@ -168,7 +169,7 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E> ...@@ -168,7 +169,7 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E>
* to maintain compatibility with previous versions * to maintain compatibility with previous versions
* of this class. Non-null only during serialization/deserialization. * of this class. Non-null only during serialization/deserialization.
*/ */
private PriorityQueue q; private PriorityQueue<E> q;
/** /**
* Creates a {@code PriorityBlockingQueue} with the default * Creates a {@code PriorityBlockingQueue} with the default
...@@ -968,7 +969,7 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E> ...@@ -968,7 +969,7 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = PriorityBlockingQueue.class; Class<?> k = PriorityBlockingQueue.class;
allocationSpinLockOffset = UNSAFE.objectFieldOffset allocationSpinLockOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("allocationSpinLock")); (k.getDeclaredField("allocationSpinLock"));
} catch (Exception e) { } catch (Exception e) {
......
...@@ -34,8 +34,10 @@ ...@@ -34,8 +34,10 @@
*/ */
package java.util.concurrent; package java.util.concurrent;
import java.util.concurrent.atomic.*; import static java.util.concurrent.TimeUnit.NANOSECONDS;
import java.util.concurrent.locks.*; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*; import java.util.*;
/** /**
...@@ -166,7 +168,7 @@ public class ScheduledThreadPoolExecutor ...@@ -166,7 +168,7 @@ public class ScheduledThreadPoolExecutor
* Sequence number to break scheduling ties, and in turn to * Sequence number to break scheduling ties, and in turn to
* guarantee FIFO order among tied entries. * guarantee FIFO order among tied entries.
*/ */
private static final AtomicLong sequencer = new AtomicLong(0); private static final AtomicLong sequencer = new AtomicLong();
/** /**
* Returns current nanosecond time. * Returns current nanosecond time.
...@@ -231,7 +233,7 @@ public class ScheduledThreadPoolExecutor ...@@ -231,7 +233,7 @@ public class ScheduledThreadPoolExecutor
} }
public long getDelay(TimeUnit unit) { public long getDelay(TimeUnit unit) {
return unit.convert(time - now(), TimeUnit.NANOSECONDS); return unit.convert(time - now(), NANOSECONDS);
} }
public int compareTo(Delayed other) { public int compareTo(Delayed other) {
...@@ -249,8 +251,8 @@ public class ScheduledThreadPoolExecutor ...@@ -249,8 +251,8 @@ public class ScheduledThreadPoolExecutor
else else
return 1; return 1;
} }
long d = (getDelay(TimeUnit.NANOSECONDS) - long d = (getDelay(NANOSECONDS) -
other.getDelay(TimeUnit.NANOSECONDS)); other.getDelay(NANOSECONDS));
return (d == 0) ? 0 : ((d < 0) ? -1 : 1); return (d == 0) ? 0 : ((d < 0) ? -1 : 1);
} }
...@@ -424,7 +426,7 @@ public class ScheduledThreadPoolExecutor ...@@ -424,7 +426,7 @@ public class ScheduledThreadPoolExecutor
* @throws IllegalArgumentException if {@code corePoolSize < 0} * @throws IllegalArgumentException if {@code corePoolSize < 0}
*/ */
public ScheduledThreadPoolExecutor(int corePoolSize) { public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue()); new DelayedWorkQueue());
} }
...@@ -441,7 +443,7 @@ public class ScheduledThreadPoolExecutor ...@@ -441,7 +443,7 @@ public class ScheduledThreadPoolExecutor
*/ */
public ScheduledThreadPoolExecutor(int corePoolSize, public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) { ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory); new DelayedWorkQueue(), threadFactory);
} }
...@@ -458,7 +460,7 @@ public class ScheduledThreadPoolExecutor ...@@ -458,7 +460,7 @@ public class ScheduledThreadPoolExecutor
*/ */
public ScheduledThreadPoolExecutor(int corePoolSize, public ScheduledThreadPoolExecutor(int corePoolSize,
RejectedExecutionHandler handler) { RejectedExecutionHandler handler) {
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), handler); new DelayedWorkQueue(), handler);
} }
...@@ -479,7 +481,7 @@ public class ScheduledThreadPoolExecutor ...@@ -479,7 +481,7 @@ public class ScheduledThreadPoolExecutor
public ScheduledThreadPoolExecutor(int corePoolSize, public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory, ThreadFactory threadFactory,
RejectedExecutionHandler handler) { RejectedExecutionHandler handler) {
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS, super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory, handler); new DelayedWorkQueue(), threadFactory, handler);
} }
...@@ -508,7 +510,7 @@ public class ScheduledThreadPoolExecutor ...@@ -508,7 +510,7 @@ public class ScheduledThreadPoolExecutor
private long overflowFree(long delay) { private long overflowFree(long delay) {
Delayed head = (Delayed) super.getQueue().peek(); Delayed head = (Delayed) super.getQueue().peek();
if (head != null) { if (head != null) {
long headDelay = head.getDelay(TimeUnit.NANOSECONDS); long headDelay = head.getDelay(NANOSECONDS);
if (headDelay < 0 && (delay - headDelay < 0)) if (headDelay < 0 && (delay - headDelay < 0))
delay = Long.MAX_VALUE + headDelay; delay = Long.MAX_VALUE + headDelay;
} }
...@@ -616,7 +618,7 @@ public class ScheduledThreadPoolExecutor ...@@ -616,7 +618,7 @@ public class ScheduledThreadPoolExecutor
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
*/ */
public void execute(Runnable command) { public void execute(Runnable command) {
schedule(command, 0, TimeUnit.NANOSECONDS); schedule(command, 0, NANOSECONDS);
} }
// Override AbstractExecutorService methods // Override AbstractExecutorService methods
...@@ -626,7 +628,7 @@ public class ScheduledThreadPoolExecutor ...@@ -626,7 +628,7 @@ public class ScheduledThreadPoolExecutor
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
*/ */
public Future<?> submit(Runnable task) { public Future<?> submit(Runnable task) {
return schedule(task, 0, TimeUnit.NANOSECONDS); return schedule(task, 0, NANOSECONDS);
} }
/** /**
...@@ -634,8 +636,7 @@ public class ScheduledThreadPoolExecutor ...@@ -634,8 +636,7 @@ public class ScheduledThreadPoolExecutor
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
*/ */
public <T> Future<T> submit(Runnable task, T result) { public <T> Future<T> submit(Runnable task, T result) {
return schedule(Executors.callable(task, result), return schedule(Executors.callable(task, result), 0, NANOSECONDS);
0, TimeUnit.NANOSECONDS);
} }
/** /**
...@@ -643,7 +644,7 @@ public class ScheduledThreadPoolExecutor ...@@ -643,7 +644,7 @@ public class ScheduledThreadPoolExecutor
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
*/ */
public <T> Future<T> submit(Callable<T> task) { public <T> Future<T> submit(Callable<T> task) {
return schedule(task, 0, TimeUnit.NANOSECONDS); return schedule(task, 0, NANOSECONDS);
} }
/** /**
...@@ -831,8 +832,8 @@ public class ScheduledThreadPoolExecutor ...@@ -831,8 +832,8 @@ public class ScheduledThreadPoolExecutor
*/ */
private static final int INITIAL_CAPACITY = 16; private static final int INITIAL_CAPACITY = 16;
private RunnableScheduledFuture[] queue = private RunnableScheduledFuture<?>[] queue =
new RunnableScheduledFuture[INITIAL_CAPACITY]; new RunnableScheduledFuture<?>[INITIAL_CAPACITY];
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
private int size = 0; private int size = 0;
...@@ -863,7 +864,7 @@ public class ScheduledThreadPoolExecutor ...@@ -863,7 +864,7 @@ public class ScheduledThreadPoolExecutor
/** /**
* Set f's heapIndex if it is a ScheduledFutureTask. * Set f's heapIndex if it is a ScheduledFutureTask.
*/ */
private void setIndex(RunnableScheduledFuture f, int idx) { private void setIndex(RunnableScheduledFuture<?> f, int idx) {
if (f instanceof ScheduledFutureTask) if (f instanceof ScheduledFutureTask)
((ScheduledFutureTask)f).heapIndex = idx; ((ScheduledFutureTask)f).heapIndex = idx;
} }
...@@ -872,10 +873,10 @@ public class ScheduledThreadPoolExecutor ...@@ -872,10 +873,10 @@ public class ScheduledThreadPoolExecutor
* Sift element added at bottom up to its heap-ordered spot. * Sift element added at bottom up to its heap-ordered spot.
* Call only when holding lock. * Call only when holding lock.
*/ */
private void siftUp(int k, RunnableScheduledFuture key) { private void siftUp(int k, RunnableScheduledFuture<?> key) {
while (k > 0) { while (k > 0) {
int parent = (k - 1) >>> 1; int parent = (k - 1) >>> 1;
RunnableScheduledFuture e = queue[parent]; RunnableScheduledFuture<?> e = queue[parent];
if (key.compareTo(e) >= 0) if (key.compareTo(e) >= 0)
break; break;
queue[k] = e; queue[k] = e;
...@@ -890,11 +891,11 @@ public class ScheduledThreadPoolExecutor ...@@ -890,11 +891,11 @@ public class ScheduledThreadPoolExecutor
* Sift element added at top down to its heap-ordered spot. * Sift element added at top down to its heap-ordered spot.
* Call only when holding lock. * Call only when holding lock.
*/ */
private void siftDown(int k, RunnableScheduledFuture key) { private void siftDown(int k, RunnableScheduledFuture<?> key) {
int half = size >>> 1; int half = size >>> 1;
while (k < half) { while (k < half) {
int child = (k << 1) + 1; int child = (k << 1) + 1;
RunnableScheduledFuture c = queue[child]; RunnableScheduledFuture<?> c = queue[child];
int right = child + 1; int right = child + 1;
if (right < size && c.compareTo(queue[right]) > 0) if (right < size && c.compareTo(queue[right]) > 0)
c = queue[child = right]; c = queue[child = right];
...@@ -959,7 +960,7 @@ public class ScheduledThreadPoolExecutor ...@@ -959,7 +960,7 @@ public class ScheduledThreadPoolExecutor
setIndex(queue[i], -1); setIndex(queue[i], -1);
int s = --size; int s = --size;
RunnableScheduledFuture replacement = queue[s]; RunnableScheduledFuture<?> replacement = queue[s];
queue[s] = null; queue[s] = null;
if (s != i) { if (s != i) {
siftDown(i, replacement); siftDown(i, replacement);
...@@ -990,7 +991,7 @@ public class ScheduledThreadPoolExecutor ...@@ -990,7 +991,7 @@ public class ScheduledThreadPoolExecutor
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
public RunnableScheduledFuture peek() { public RunnableScheduledFuture<?> peek() {
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
...@@ -1003,7 +1004,7 @@ public class ScheduledThreadPoolExecutor ...@@ -1003,7 +1004,7 @@ public class ScheduledThreadPoolExecutor
public boolean offer(Runnable x) { public boolean offer(Runnable x) {
if (x == null) if (x == null)
throw new NullPointerException(); throw new NullPointerException();
RunnableScheduledFuture e = (RunnableScheduledFuture)x; RunnableScheduledFuture<?> e = (RunnableScheduledFuture<?>)x;
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
...@@ -1045,9 +1046,9 @@ public class ScheduledThreadPoolExecutor ...@@ -1045,9 +1046,9 @@ public class ScheduledThreadPoolExecutor
* holding lock. * holding lock.
* @param f the task to remove and return * @param f the task to remove and return
*/ */
private RunnableScheduledFuture finishPoll(RunnableScheduledFuture f) { private RunnableScheduledFuture<?> finishPoll(RunnableScheduledFuture<?> f) {
int s = --size; int s = --size;
RunnableScheduledFuture x = queue[s]; RunnableScheduledFuture<?> x = queue[s];
queue[s] = null; queue[s] = null;
if (s != 0) if (s != 0)
siftDown(0, x); siftDown(0, x);
...@@ -1055,12 +1056,12 @@ public class ScheduledThreadPoolExecutor ...@@ -1055,12 +1056,12 @@ public class ScheduledThreadPoolExecutor
return f; return f;
} }
public RunnableScheduledFuture poll() { public RunnableScheduledFuture<?> poll() {
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
RunnableScheduledFuture first = queue[0]; RunnableScheduledFuture<?> first = queue[0];
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) if (first == null || first.getDelay(NANOSECONDS) > 0)
return null; return null;
else else
return finishPoll(first); return finishPoll(first);
...@@ -1069,16 +1070,16 @@ public class ScheduledThreadPoolExecutor ...@@ -1069,16 +1070,16 @@ public class ScheduledThreadPoolExecutor
} }
} }
public RunnableScheduledFuture take() throws InterruptedException { public RunnableScheduledFuture<?> take() throws InterruptedException {
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lockInterruptibly(); lock.lockInterruptibly();
try { try {
for (;;) { for (;;) {
RunnableScheduledFuture first = queue[0]; RunnableScheduledFuture<?> first = queue[0];
if (first == null) if (first == null)
available.await(); available.await();
else { else {
long delay = first.getDelay(TimeUnit.NANOSECONDS); long delay = first.getDelay(NANOSECONDS);
if (delay <= 0) if (delay <= 0)
return finishPoll(first); return finishPoll(first);
else if (leader != null) else if (leader != null)
...@@ -1102,21 +1103,21 @@ public class ScheduledThreadPoolExecutor ...@@ -1102,21 +1103,21 @@ public class ScheduledThreadPoolExecutor
} }
} }
public RunnableScheduledFuture poll(long timeout, TimeUnit unit) public RunnableScheduledFuture<?> poll(long timeout, TimeUnit unit)
throws InterruptedException { throws InterruptedException {
long nanos = unit.toNanos(timeout); long nanos = unit.toNanos(timeout);
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lockInterruptibly(); lock.lockInterruptibly();
try { try {
for (;;) { for (;;) {
RunnableScheduledFuture first = queue[0]; RunnableScheduledFuture<?> first = queue[0];
if (first == null) { if (first == null) {
if (nanos <= 0) if (nanos <= 0)
return null; return null;
else else
nanos = available.awaitNanos(nanos); nanos = available.awaitNanos(nanos);
} else { } else {
long delay = first.getDelay(TimeUnit.NANOSECONDS); long delay = first.getDelay(NANOSECONDS);
if (delay <= 0) if (delay <= 0)
return finishPoll(first); return finishPoll(first);
if (nanos <= 0) if (nanos <= 0)
...@@ -1148,7 +1149,7 @@ public class ScheduledThreadPoolExecutor ...@@ -1148,7 +1149,7 @@ public class ScheduledThreadPoolExecutor
lock.lock(); lock.lock();
try { try {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
RunnableScheduledFuture t = queue[i]; RunnableScheduledFuture<?> t = queue[i];
if (t != null) { if (t != null) {
queue[i] = null; queue[i] = null;
setIndex(t, -1); setIndex(t, -1);
...@@ -1164,9 +1165,10 @@ public class ScheduledThreadPoolExecutor ...@@ -1164,9 +1165,10 @@ public class ScheduledThreadPoolExecutor
* Return and remove first element only if it is expired. * Return and remove first element only if it is expired.
* Used only by drainTo. Call only when holding lock. * Used only by drainTo. Call only when holding lock.
*/ */
private RunnableScheduledFuture pollExpired() { private RunnableScheduledFuture<?> pollExpired() {
RunnableScheduledFuture first = queue[0]; // assert lock.isHeldByCurrentThread();
if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0) RunnableScheduledFuture<?> first = queue[0];
if (first == null || first.getDelay(NANOSECONDS) > 0)
return null; return null;
return finishPoll(first); return finishPoll(first);
} }
...@@ -1179,7 +1181,7 @@ public class ScheduledThreadPoolExecutor ...@@ -1179,7 +1181,7 @@ public class ScheduledThreadPoolExecutor
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
RunnableScheduledFuture first; RunnableScheduledFuture<?> first;
int n = 0; int n = 0;
while ((first = pollExpired()) != null) { while ((first = pollExpired()) != null) {
c.add(first); c.add(first);
...@@ -1201,7 +1203,7 @@ public class ScheduledThreadPoolExecutor ...@@ -1201,7 +1203,7 @@ public class ScheduledThreadPoolExecutor
final ReentrantLock lock = this.lock; final ReentrantLock lock = this.lock;
lock.lock(); lock.lock();
try { try {
RunnableScheduledFuture first; RunnableScheduledFuture<?> first;
int n = 0; int n = 0;
while (n < maxElements && (first = pollExpired()) != null) { while (n < maxElements && (first = pollExpired()) != null) {
c.add(first); c.add(first);
......
...@@ -36,7 +36,6 @@ ...@@ -36,7 +36,6 @@
package java.util.concurrent; package java.util.concurrent;
import java.util.concurrent.locks.*; import java.util.concurrent.locks.*;
import java.util.concurrent.atomic.*;
import java.util.*; import java.util.*;
/** /**
...@@ -163,7 +162,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -163,7 +162,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
/** /**
* Shared internal API for dual stacks and queues. * Shared internal API for dual stacks and queues.
*/ */
abstract static class Transferer { abstract static class Transferer<E> {
/** /**
* Performs a put or take. * Performs a put or take.
* *
...@@ -177,7 +176,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -177,7 +176,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* the caller can distinguish which of these occurred * the caller can distinguish which of these occurred
* by checking Thread.interrupted. * by checking Thread.interrupted.
*/ */
abstract Object transfer(Object e, boolean timed, long nanos); abstract E transfer(E e, boolean timed, long nanos);
} }
/** The number of CPUs, for spin control */ /** The number of CPUs, for spin control */
...@@ -206,7 +205,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -206,7 +205,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
static final long spinForTimeoutThreshold = 1000L; static final long spinForTimeoutThreshold = 1000L;
/** Dual stack */ /** Dual stack */
static final class TransferStack extends Transferer { static final class TransferStack<E> extends Transferer<E> {
/* /*
* This extends Scherer-Scott dual stack algorithm, differing, * This extends Scherer-Scott dual stack algorithm, differing,
* among other ways, by using "covering" nodes rather than * among other ways, by using "covering" nodes rather than
...@@ -286,7 +285,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -286,7 +285,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = SNode.class; Class<?> k = SNode.class;
matchOffset = UNSAFE.objectFieldOffset matchOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("match")); (k.getDeclaredField("match"));
nextOffset = UNSAFE.objectFieldOffset nextOffset = UNSAFE.objectFieldOffset
...@@ -322,7 +321,8 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -322,7 +321,8 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
/** /**
* Puts or takes an item. * Puts or takes an item.
*/ */
Object transfer(Object e, boolean timed, long nanos) { @SuppressWarnings("unchecked")
E transfer(E e, boolean timed, long nanos) {
/* /*
* Basic algorithm is to loop trying one of three actions: * Basic algorithm is to loop trying one of three actions:
* *
...@@ -363,7 +363,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -363,7 +363,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
} }
if ((h = head) != null && h.next == s) if ((h = head) != null && h.next == s)
casHead(h, s.next); // help s's fulfiller casHead(h, s.next); // help s's fulfiller
return (mode == REQUEST) ? m.item : s.item; return (E) ((mode == REQUEST) ? m.item : s.item);
} }
} else if (!isFulfilling(h.mode)) { // try to fulfill } else if (!isFulfilling(h.mode)) { // try to fulfill
if (h.isCancelled()) // already cancelled if (h.isCancelled()) // already cancelled
...@@ -379,7 +379,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -379,7 +379,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
SNode mn = m.next; SNode mn = m.next;
if (m.tryMatch(s)) { if (m.tryMatch(s)) {
casHead(s, mn); // pop both s and m casHead(s, mn); // pop both s and m
return (mode == REQUEST) ? m.item : s.item; return (E) ((mode == REQUEST) ? m.item : s.item);
} else // lost match } else // lost match
s.casNext(m, mn); // help unlink s.casNext(m, mn); // help unlink
} }
...@@ -513,7 +513,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -513,7 +513,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = TransferStack.class; Class<?> k = TransferStack.class;
headOffset = UNSAFE.objectFieldOffset headOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("head")); (k.getDeclaredField("head"));
} catch (Exception e) { } catch (Exception e) {
...@@ -523,7 +523,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -523,7 +523,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
} }
/** Dual Queue */ /** Dual Queue */
static final class TransferQueue extends Transferer { static final class TransferQueue<E> extends Transferer<E> {
/* /*
* This extends Scherer-Scott dual queue algorithm, differing, * This extends Scherer-Scott dual queue algorithm, differing,
* among other ways, by using modes within nodes rather than * among other ways, by using modes within nodes rather than
...@@ -583,7 +583,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -583,7 +583,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = QNode.class; Class<?> k = QNode.class;
itemOffset = UNSAFE.objectFieldOffset itemOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("item")); (k.getDeclaredField("item"));
nextOffset = UNSAFE.objectFieldOffset nextOffset = UNSAFE.objectFieldOffset
...@@ -640,7 +640,8 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -640,7 +640,8 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
/** /**
* Puts or takes an item. * Puts or takes an item.
*/ */
Object transfer(Object e, boolean timed, long nanos) { @SuppressWarnings("unchecked")
E transfer(E e, boolean timed, long nanos) {
/* Basic algorithm is to loop trying to take either of /* Basic algorithm is to loop trying to take either of
* two actions: * two actions:
* *
...@@ -703,7 +704,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -703,7 +704,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
s.item = s; s.item = s;
s.waiter = null; s.waiter = null;
} }
return (x != null) ? x : e; return (x != null) ? (E)x : e;
} else { // complementary-mode } else { // complementary-mode
QNode m = h.next; // node to fulfill QNode m = h.next; // node to fulfill
...@@ -720,7 +721,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -720,7 +721,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
advanceHead(h, m); // successfully fulfilled advanceHead(h, m); // successfully fulfilled
LockSupport.unpark(m.waiter); LockSupport.unpark(m.waiter);
return (x != null) ? x : e; return (x != null) ? (E)x : e;
} }
} }
} }
...@@ -734,7 +735,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -734,7 +735,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* @param nanos timeout value * @param nanos timeout value
* @return matched item, or s if cancelled * @return matched item, or s if cancelled
*/ */
Object awaitFulfill(QNode s, Object e, boolean timed, long nanos) { Object awaitFulfill(QNode s, E e, boolean timed, long nanos) {
/* Same idea as TransferStack.awaitFulfill */ /* Same idea as TransferStack.awaitFulfill */
long lastTime = timed ? System.nanoTime() : 0; long lastTime = timed ? System.nanoTime() : 0;
Thread w = Thread.currentThread(); Thread w = Thread.currentThread();
...@@ -827,7 +828,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -827,7 +828,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
static { static {
try { try {
UNSAFE = sun.misc.Unsafe.getUnsafe(); UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = TransferQueue.class; Class<?> k = TransferQueue.class;
headOffset = UNSAFE.objectFieldOffset headOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("head")); (k.getDeclaredField("head"));
tailOffset = UNSAFE.objectFieldOffset tailOffset = UNSAFE.objectFieldOffset
...@@ -847,7 +848,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -847,7 +848,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* isn't a noticeable performance penalty for using volatile * isn't a noticeable performance penalty for using volatile
* instead of final here. * instead of final here.
*/ */
private transient volatile Transferer transferer; private transient volatile Transferer<E> transferer;
/** /**
* Creates a <tt>SynchronousQueue</tt> with nonfair access policy. * Creates a <tt>SynchronousQueue</tt> with nonfair access policy.
...@@ -863,7 +864,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -863,7 +864,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* access; otherwise the order is unspecified. * access; otherwise the order is unspecified.
*/ */
public SynchronousQueue(boolean fair) { public SynchronousQueue(boolean fair) {
transferer = fair ? new TransferQueue() : new TransferStack(); transferer = fair ? new TransferQueue<E>() : new TransferStack<E>();
} }
/** /**
...@@ -922,9 +923,9 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -922,9 +923,9 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* @throws InterruptedException {@inheritDoc} * @throws InterruptedException {@inheritDoc}
*/ */
public E take() throws InterruptedException { public E take() throws InterruptedException {
Object e = transferer.transfer(null, false, 0); E e = transferer.transfer(null, false, 0);
if (e != null) if (e != null)
return (E)e; return e;
Thread.interrupted(); Thread.interrupted();
throw new InterruptedException(); throw new InterruptedException();
} }
...@@ -939,9 +940,9 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -939,9 +940,9 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* @throws InterruptedException {@inheritDoc} * @throws InterruptedException {@inheritDoc}
*/ */
public E poll(long timeout, TimeUnit unit) throws InterruptedException { public E poll(long timeout, TimeUnit unit) throws InterruptedException {
Object e = transferer.transfer(null, true, unit.toNanos(timeout)); E e = transferer.transfer(null, true, unit.toNanos(timeout));
if (e != null || !Thread.interrupted()) if (e != null || !Thread.interrupted())
return (E)e; return e;
throw new InterruptedException(); throw new InterruptedException();
} }
...@@ -953,7 +954,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -953,7 +954,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* element is available. * element is available.
*/ */
public E poll() { public E poll() {
return (E)transferer.transfer(null, true, 0); return transferer.transfer(null, true, 0);
} }
/** /**
...@@ -1065,8 +1066,19 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -1065,8 +1066,19 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* *
* @return an empty iterator * @return an empty iterator
*/ */
@SuppressWarnings("unchecked")
public Iterator<E> iterator() { public Iterator<E> iterator() {
return Collections.emptyIterator(); return (Iterator<E>) EmptyIterator.EMPTY_ITERATOR;
}
// Replicated from a previous version of Collections
private static class EmptyIterator<E> implements Iterator<E> {
static final EmptyIterator<Object> EMPTY_ITERATOR
= new EmptyIterator<Object>();
public boolean hasNext() { return false; }
public E next() { throw new NoSuchElementException(); }
public void remove() { throw new IllegalStateException(); }
} }
/** /**
...@@ -1103,8 +1115,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -1103,8 +1115,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
if (c == this) if (c == this)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
int n = 0; int n = 0;
E e; for (E e; (e = poll()) != null;) {
while ( (e = poll()) != null) {
c.add(e); c.add(e);
++n; ++n;
} }
...@@ -1123,8 +1134,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -1123,8 +1134,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
if (c == this) if (c == this)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
int n = 0; int n = 0;
E e; for (E e; n < maxElements && (e = poll()) != null;) {
while (n < maxElements && (e = poll()) != null) {
c.add(e); c.add(e);
++n; ++n;
} }
...@@ -1139,6 +1149,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -1139,6 +1149,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
* object is ever serialized or deserialized. * object is ever serialized or deserialized.
*/ */
@SuppressWarnings("serial")
static class WaitQueue implements java.io.Serializable { } static class WaitQueue implements java.io.Serializable { }
static class LifoWaitQueue extends WaitQueue { static class LifoWaitQueue extends WaitQueue {
private static final long serialVersionUID = -3633113410248163686L; private static final long serialVersionUID = -3633113410248163686L;
...@@ -1151,7 +1162,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -1151,7 +1162,7 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
private WaitQueue waitingConsumers; private WaitQueue waitingConsumers;
/** /**
* Save the state to a stream (that is, serialize it). * Saves the state to a stream (that is, serializes it).
* *
* @param s the stream * @param s the stream
*/ */
...@@ -1175,9 +1186,9 @@ public class SynchronousQueue<E> extends AbstractQueue<E> ...@@ -1175,9 +1186,9 @@ public class SynchronousQueue<E> extends AbstractQueue<E>
throws java.io.IOException, ClassNotFoundException { throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); s.defaultReadObject();
if (waitingProducers instanceof FifoWaitQueue) if (waitingProducers instanceof FifoWaitQueue)
transferer = new TransferQueue(); transferer = new TransferQueue<E>();
else else
transferer = new TransferStack(); transferer = new TransferStack<E>();
} }
// Unsafe mechanics // Unsafe mechanics
......
...@@ -35,8 +35,6 @@ public class EmptyIterator { ...@@ -35,8 +35,6 @@ public class EmptyIterator {
void test(String[] args) throws Throwable { void test(String[] args) throws Throwable {
testEmptyCollection(Collections.<Object>emptyList()); testEmptyCollection(Collections.<Object>emptyList());
testEmptyCollection(Collections.<Object>emptySet()); testEmptyCollection(Collections.<Object>emptySet());
testEmptyCollection(new java.util.concurrent.
SynchronousQueue<Object>());
testEmptyMap(Collections.<Object, Object>emptyMap()); testEmptyMap(Collections.<Object, Object>emptyMap());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册