88 |
|
import java.util.concurrent.ThreadFactory; |
89 |
|
import java.util.concurrent.ThreadLocalRandom; |
90 |
|
import java.util.concurrent.ThreadPoolExecutor; |
91 |
+ |
import java.util.concurrent.TimeUnit; |
92 |
|
import java.util.concurrent.TimeoutException; |
93 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
94 |
|
import java.util.concurrent.atomic.AtomicReference; |
639 |
|
public static long MEDIUM_DELAY_MS; |
640 |
|
public static long LONG_DELAY_MS; |
641 |
|
|
642 |
+ |
private static final long RANDOM_TIMEOUT; |
643 |
+ |
private static final long RANDOM_EXPIRED_TIMEOUT; |
644 |
+ |
private static final TimeUnit RANDOM_TIMEUNIT; |
645 |
+ |
static { |
646 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
647 |
+ |
long[] timeouts = { Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE }; |
648 |
+ |
RANDOM_TIMEOUT = timeouts[rnd.nextInt(timeouts.length)]; |
649 |
+ |
RANDOM_EXPIRED_TIMEOUT = timeouts[rnd.nextInt(3)]; |
650 |
+ |
TimeUnit[] timeUnits = TimeUnit.values(); |
651 |
+ |
RANDOM_TIMEUNIT = timeUnits[rnd.nextInt(timeUnits.length)]; |
652 |
+ |
} |
653 |
+ |
|
654 |
+ |
/** |
655 |
+ |
* Returns a timeout for use when any value at all will do. |
656 |
+ |
*/ |
657 |
+ |
static long randomTimeout() { return RANDOM_TIMEOUT; } |
658 |
+ |
|
659 |
+ |
/** |
660 |
+ |
* Returns a timeout that means "no waiting", i.e. not positive. |
661 |
+ |
*/ |
662 |
+ |
static long randomExpiredTimeout() { return RANDOM_EXPIRED_TIMEOUT; } |
663 |
+ |
|
664 |
+ |
/** |
665 |
+ |
* Returns a random non-null TimeUnit. |
666 |
+ |
*/ |
667 |
+ |
static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; } |
668 |
+ |
|
669 |
|
/** |
670 |
|
* Returns the shortest timed delay. This can be scaled up for |
671 |
|
* slow machines using the jsr166.delay.factor system property, |
1700 |
|
public String call() { throw new NullPointerException(); } |
1701 |
|
} |
1702 |
|
|
1675 |
– |
public static class CallableOne implements Callable<Integer> { |
1676 |
– |
public Integer call() { return one; } |
1677 |
– |
} |
1678 |
– |
|
1679 |
– |
public class ShortRunnable extends CheckedRunnable { |
1680 |
– |
protected void realRun() throws Throwable { |
1681 |
– |
delay(SHORT_DELAY_MS); |
1682 |
– |
} |
1683 |
– |
} |
1684 |
– |
|
1685 |
– |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1686 |
– |
protected void realRun() throws InterruptedException { |
1687 |
– |
delay(SHORT_DELAY_MS); |
1688 |
– |
} |
1689 |
– |
} |
1690 |
– |
|
1691 |
– |
public class SmallRunnable extends CheckedRunnable { |
1692 |
– |
protected void realRun() throws Throwable { |
1693 |
– |
delay(SMALL_DELAY_MS); |
1694 |
– |
} |
1695 |
– |
} |
1696 |
– |
|
1703 |
|
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1704 |
|
protected void realRun() { |
1705 |
|
try { |
1708 |
|
} |
1709 |
|
} |
1710 |
|
|
1705 |
– |
public class SmallCallable extends CheckedCallable { |
1706 |
– |
protected Object realCall() throws InterruptedException { |
1707 |
– |
delay(SMALL_DELAY_MS); |
1708 |
– |
return Boolean.TRUE; |
1709 |
– |
} |
1710 |
– |
} |
1711 |
– |
|
1712 |
– |
public class MediumRunnable extends CheckedRunnable { |
1713 |
– |
protected void realRun() throws Throwable { |
1714 |
– |
delay(MEDIUM_DELAY_MS); |
1715 |
– |
} |
1716 |
– |
} |
1717 |
– |
|
1718 |
– |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1719 |
– |
protected void realRun() throws InterruptedException { |
1720 |
– |
delay(MEDIUM_DELAY_MS); |
1721 |
– |
} |
1722 |
– |
} |
1723 |
– |
|
1711 |
|
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1712 |
|
return new CheckedRunnable() { |
1713 |
|
protected void realRun() { |
1717 |
|
}}; |
1718 |
|
} |
1719 |
|
|
1733 |
– |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1734 |
– |
protected void realRun() { |
1735 |
– |
try { |
1736 |
– |
delay(MEDIUM_DELAY_MS); |
1737 |
– |
} catch (InterruptedException ok) {} |
1738 |
– |
} |
1739 |
– |
} |
1740 |
– |
|
1741 |
– |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1742 |
– |
protected void realRun() { |
1743 |
– |
try { |
1744 |
– |
delay(LONG_DELAY_MS); |
1745 |
– |
} catch (InterruptedException ok) {} |
1746 |
– |
} |
1747 |
– |
} |
1748 |
– |
|
1720 |
|
/** |
1721 |
|
* For use as ThreadFactory in constructors |
1722 |
|
*/ |
1730 |
|
boolean isDone(); |
1731 |
|
} |
1732 |
|
|
1762 |
– |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1763 |
– |
return new TrackedRunnable() { |
1764 |
– |
private volatile boolean done = false; |
1765 |
– |
public boolean isDone() { return done; } |
1766 |
– |
public void run() { |
1767 |
– |
try { |
1768 |
– |
delay(timeoutMillis); |
1769 |
– |
done = true; |
1770 |
– |
} catch (InterruptedException ok) {} |
1771 |
– |
} |
1772 |
– |
}; |
1773 |
– |
} |
1774 |
– |
|
1775 |
– |
public static class TrackedShortRunnable implements Runnable { |
1776 |
– |
public volatile boolean done = false; |
1777 |
– |
public void run() { |
1778 |
– |
try { |
1779 |
– |
delay(SHORT_DELAY_MS); |
1780 |
– |
done = true; |
1781 |
– |
} catch (InterruptedException ok) {} |
1782 |
– |
} |
1783 |
– |
} |
1784 |
– |
|
1785 |
– |
public static class TrackedSmallRunnable implements Runnable { |
1786 |
– |
public volatile boolean done = false; |
1787 |
– |
public void run() { |
1788 |
– |
try { |
1789 |
– |
delay(SMALL_DELAY_MS); |
1790 |
– |
done = true; |
1791 |
– |
} catch (InterruptedException ok) {} |
1792 |
– |
} |
1793 |
– |
} |
1794 |
– |
|
1795 |
– |
public static class TrackedMediumRunnable implements Runnable { |
1796 |
– |
public volatile boolean done = false; |
1797 |
– |
public void run() { |
1798 |
– |
try { |
1799 |
– |
delay(MEDIUM_DELAY_MS); |
1800 |
– |
done = true; |
1801 |
– |
} catch (InterruptedException ok) {} |
1802 |
– |
} |
1803 |
– |
} |
1804 |
– |
|
1805 |
– |
public static class TrackedLongRunnable implements Runnable { |
1806 |
– |
public volatile boolean done = false; |
1807 |
– |
public void run() { |
1808 |
– |
try { |
1809 |
– |
delay(LONG_DELAY_MS); |
1810 |
– |
done = true; |
1811 |
– |
} catch (InterruptedException ok) {} |
1812 |
– |
} |
1813 |
– |
} |
1814 |
– |
|
1733 |
|
public static class TrackedNoOpRunnable implements Runnable { |
1734 |
|
public volatile boolean done = false; |
1735 |
|
public void run() { |
1737 |
|
} |
1738 |
|
} |
1739 |
|
|
1822 |
– |
public static class TrackedCallable implements Callable { |
1823 |
– |
public volatile boolean done = false; |
1824 |
– |
public Object call() { |
1825 |
– |
try { |
1826 |
– |
delay(SMALL_DELAY_MS); |
1827 |
– |
done = true; |
1828 |
– |
} catch (InterruptedException ok) {} |
1829 |
– |
return Boolean.TRUE; |
1830 |
– |
} |
1831 |
– |
} |
1832 |
– |
|
1740 |
|
/** |
1741 |
|
* Analog of CheckedRunnable for RecursiveAction |
1742 |
|
*/ |
1803 |
|
assertEquals(0, q.size()); |
1804 |
|
assertNull(q.peek()); |
1805 |
|
assertNull(q.poll()); |
1806 |
< |
assertNull(q.poll(0, MILLISECONDS)); |
1806 |
> |
assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); |
1807 |
|
assertEquals(q.toString(), "[]"); |
1808 |
|
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1809 |
|
assertFalse(q.iterator().hasNext()); |