56 |
|
* maximum number of running threads to 32767. Attempts to create |
57 |
|
* pools with greater than the maximum result in |
58 |
|
* IllegalArgumentExceptions. |
59 |
+ |
* |
60 |
+ |
* @since 1.7 |
61 |
+ |
* @author Doug Lea |
62 |
|
*/ |
63 |
|
public class ForkJoinPool extends AbstractExecutorService { |
64 |
|
|
84 |
|
* Returns a new worker thread operating in the given pool. |
85 |
|
* |
86 |
|
* @param pool the pool this thread works in |
87 |
< |
* @throws NullPointerException if pool is null; |
87 |
> |
* @throws NullPointerException if pool is null |
88 |
|
*/ |
89 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool pool); |
90 |
|
} |
276 |
|
/** |
277 |
|
* Try incrementing active count; fail on contention. Called by |
278 |
|
* workers before/during executing tasks. |
279 |
< |
* @return true on success; |
279 |
> |
* @return true on success |
280 |
|
*/ |
281 |
|
final boolean tryIncrementActiveCount() { |
282 |
|
int c = runControl; |
379 |
|
* @param parallelism the targeted number of worker threads |
380 |
|
* @param factory the factory for creating new threads |
381 |
|
* @throws IllegalArgumentException if parallelism less than or |
382 |
< |
* equal to zero, or greater than implementation limit. |
382 |
> |
* equal to zero, or greater than implementation limit |
383 |
|
* @throws NullPointerException if factory is null |
384 |
|
* @throws SecurityException if a security manager exists and |
385 |
|
* the caller is not permitted to modify threads |
722 |
|
* Sets the target parallelism level of this pool. |
723 |
|
* @param parallelism the target parallelism |
724 |
|
* @throws IllegalArgumentException if parallelism less than or |
725 |
< |
* equal to zero or greater than maximum size bounds. |
725 |
> |
* equal to zero or greater than maximum size bounds |
726 |
|
* @throws SecurityException if a security manager exists and |
727 |
|
* the caller is not permitted to modify threads |
728 |
|
* because it does not hold {@link |
785 |
|
* Setting this value has no effect on current pool size. It |
786 |
|
* controls construction of new threads. |
787 |
|
* @throws IllegalArgumentException if negative or greater then |
788 |
< |
* internal implementation limit. |
788 |
> |
* internal implementation limit |
789 |
|
*/ |
790 |
|
public void setMaximumPoolSize(int newMax) { |
791 |
|
if (newMax < 0 || newMax > MAX_THREADS) |
825 |
|
* invocations at other times may be unpredictable. |
826 |
|
* |
827 |
|
* @param async if true, use locally FIFO scheduling |
828 |
< |
* @return the previous mode. |
828 |
> |
* @return the previous mode |
829 |
|
*/ |
830 |
|
public boolean setAsyncMode(boolean async) { |
831 |
|
boolean oldMode = locallyFifo; |
845 |
|
* Returns true if this pool uses local first-in-first-out |
846 |
|
* scheduling mode for forked tasks that are never joined. |
847 |
|
* |
848 |
< |
* @return true if this pool uses async mode. |
848 |
> |
* @return true if this pool uses async mode |
849 |
|
*/ |
850 |
|
public boolean getAsyncMode() { |
851 |
|
return locallyFifo; |
866 |
|
* Returns an estimate of the number of threads that are currently |
867 |
|
* stealing or executing tasks. This method may overestimate the |
868 |
|
* number of active threads. |
869 |
< |
* @return the number of active threads. |
869 |
> |
* @return the number of active threads |
870 |
|
*/ |
871 |
|
public int getActiveThreadCount() { |
872 |
|
return activeCountOf(runControl); |
876 |
|
* Returns an estimate of the number of threads that are currently |
877 |
|
* idle waiting for tasks. This method may underestimate the |
878 |
|
* number of idle threads. |
879 |
< |
* @return the number of idle threads. |
879 |
> |
* @return the number of idle threads |
880 |
|
*/ |
881 |
|
final int getIdleThreadCount() { |
882 |
|
int c = runningCountOf(workerCounts) - activeCountOf(runControl); |
905 |
|
* tuning fork/join programs: In general, steal counts should be |
906 |
|
* high enough to keep threads busy, but low enough to avoid |
907 |
|
* overhead and contention across threads. |
908 |
< |
* @return the number of steals. |
908 |
> |
* @return the number of steals |
909 |
|
*/ |
910 |
|
public long getStealCount() { |
911 |
|
return stealCount.get(); |
928 |
|
* an approximation, obtained by iterating across all threads in |
929 |
|
* the pool. This method may be useful for tuning task |
930 |
|
* granularities. |
931 |
< |
* @return the number of queued tasks. |
931 |
> |
* @return the number of queued tasks |
932 |
|
*/ |
933 |
|
public long getQueuedTaskCount() { |
934 |
|
long count = 0; |
947 |
|
* Returns an estimate of the number tasks submitted to this pool |
948 |
|
* that have not yet begun executing. This method takes time |
949 |
|
* proportional to the number of submissions. |
950 |
< |
* @return the number of queued submissions. |
950 |
> |
* @return the number of queued submissions |
951 |
|
*/ |
952 |
|
public int getQueuedSubmissionCount() { |
953 |
|
return submissionQueue.size(); |
956 |
|
/** |
957 |
|
* Returns true if there are any tasks submitted to this pool |
958 |
|
* that have not yet begun executing. |
959 |
< |
* @return {@code true} if there are any queued submissions. |
959 |
> |
* @return {@code true} if there are any queued submissions |
960 |
|
*/ |
961 |
|
public boolean hasQueuedSubmissions() { |
962 |
|
return !submissionQueue.isEmpty(); |
1720 |
|
* Possibly blocks the current thread, for example waiting for |
1721 |
|
* a lock or condition. |
1722 |
|
* @return true if no additional blocking is necessary (i.e., |
1723 |
< |
* if isReleasable would return true). |
1723 |
> |
* if isReleasable would return true) |
1724 |
|
* @throws InterruptedException if interrupted while waiting |
1725 |
|
* (the method is not required to do so, but is allowed to). |
1726 |
|
*/ |
1759 |
|
* attempt to maintain the pool's nominal parallelism; otherwise |
1760 |
|
* activate a thread only if necessary to avoid complete |
1761 |
|
* starvation. |
1762 |
< |
* @throws InterruptedException if blocker.block did so. |
1762 |
> |
* @throws InterruptedException if blocker.block did so |
1763 |
|
*/ |
1764 |
|
public static void managedBlock(ManagedBlocker blocker, |
1765 |
|
boolean maintainParallelism) |
1821 |
|
|
1822 |
|
private static long fieldOffset(String fieldName) |
1823 |
|
throws NoSuchFieldException { |
1824 |
< |
return _unsafe.objectFieldOffset |
1824 |
> |
return UNSAFE.objectFieldOffset |
1825 |
|
(ForkJoinPool.class.getDeclaredField(fieldName)); |
1826 |
|
} |
1827 |
|
|
1828 |
< |
static final Unsafe _unsafe; |
1828 |
> |
static final Unsafe UNSAFE; |
1829 |
|
static final long eventCountOffset; |
1830 |
|
static final long workerCountsOffset; |
1831 |
|
static final long runControlOffset; |
1834 |
|
|
1835 |
|
static { |
1836 |
|
try { |
1837 |
< |
_unsafe = getUnsafe(); |
1837 |
> |
UNSAFE = getUnsafe(); |
1838 |
|
eventCountOffset = fieldOffset("eventCount"); |
1839 |
|
workerCountsOffset = fieldOffset("workerCounts"); |
1840 |
|
runControlOffset = fieldOffset("runControl"); |
1846 |
|
} |
1847 |
|
|
1848 |
|
private boolean casEventCount(long cmp, long val) { |
1849 |
< |
return _unsafe.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1849 |
> |
return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1850 |
|
} |
1851 |
|
private boolean casWorkerCounts(int cmp, int val) { |
1852 |
< |
return _unsafe.compareAndSwapInt(this, workerCountsOffset, cmp, val); |
1852 |
> |
return UNSAFE.compareAndSwapInt(this, workerCountsOffset, cmp, val); |
1853 |
|
} |
1854 |
|
private boolean casRunControl(int cmp, int val) { |
1855 |
< |
return _unsafe.compareAndSwapInt(this, runControlOffset, cmp, val); |
1855 |
> |
return UNSAFE.compareAndSwapInt(this, runControlOffset, cmp, val); |
1856 |
|
} |
1857 |
|
private boolean casSpareStack(WaitQueueNode cmp, WaitQueueNode val) { |
1858 |
< |
return _unsafe.compareAndSwapObject(this, spareStackOffset, cmp, val); |
1858 |
> |
return UNSAFE.compareAndSwapObject(this, spareStackOffset, cmp, val); |
1859 |
|
} |
1860 |
|
private boolean casBarrierStack(WaitQueueNode cmp, WaitQueueNode val) { |
1861 |
< |
return _unsafe.compareAndSwapObject(this, syncStackOffset, cmp, val); |
1861 |
> |
return UNSAFE.compareAndSwapObject(this, syncStackOffset, cmp, val); |
1862 |
|
} |
1863 |
|
} |