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 |
|
} |
219 |
|
* threads, packed into one int to ensure consistent snapshot when |
220 |
|
* making decisions about creating and suspending spare |
221 |
|
* threads. Updated only by CAS. Note: CASes in |
222 |
< |
* updateRunningCount and preJoin running active count is in low |
223 |
< |
* word, so need to be modified if this changes |
222 |
> |
* updateRunningCount and preJoin assume that running active count |
223 |
> |
* is in low word, so need to be modified if this changes. |
224 |
|
*/ |
225 |
|
private volatile int workerCounts; |
226 |
|
|
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 |
433 |
|
|
434 |
|
/** |
435 |
|
* Creates or resizes array if necessary to hold newLength. |
436 |
< |
* Call only under exclusion or lock. |
436 |
> |
* Call only under exclusion. |
437 |
> |
* |
438 |
|
* @return the array |
439 |
|
*/ |
440 |
|
private ForkJoinWorkerThread[] ensureWorkerArrayCapacity(int newLength) { |
723 |
|
* Sets the target parallelism level of this pool. |
724 |
|
* @param parallelism the target parallelism |
725 |
|
* @throws IllegalArgumentException if parallelism less than or |
726 |
< |
* equal to zero or greater than maximum size bounds. |
726 |
> |
* equal to zero or greater than maximum size bounds |
727 |
|
* @throws SecurityException if a security manager exists and |
728 |
|
* the caller is not permitted to modify threads |
729 |
|
* because it does not hold {@link |
786 |
|
* Setting this value has no effect on current pool size. It |
787 |
|
* controls construction of new threads. |
788 |
|
* @throws IllegalArgumentException if negative or greater then |
789 |
< |
* internal implementation limit. |
789 |
> |
* internal implementation limit |
790 |
|
*/ |
791 |
|
public void setMaximumPoolSize(int newMax) { |
792 |
|
if (newMax < 0 || newMax > MAX_THREADS) |
826 |
|
* invocations at other times may be unpredictable. |
827 |
|
* |
828 |
|
* @param async if true, use locally FIFO scheduling |
829 |
< |
* @return the previous mode. |
829 |
> |
* @return the previous mode |
830 |
|
*/ |
831 |
|
public boolean setAsyncMode(boolean async) { |
832 |
|
boolean oldMode = locallyFifo; |
846 |
|
* Returns true if this pool uses local first-in-first-out |
847 |
|
* scheduling mode for forked tasks that are never joined. |
848 |
|
* |
849 |
< |
* @return true if this pool uses async mode. |
849 |
> |
* @return true if this pool uses async mode |
850 |
|
*/ |
851 |
|
public boolean getAsyncMode() { |
852 |
|
return locallyFifo; |
867 |
|
* Returns an estimate of the number of threads that are currently |
868 |
|
* stealing or executing tasks. This method may overestimate the |
869 |
|
* number of active threads. |
870 |
< |
* @return the number of active threads. |
870 |
> |
* @return the number of active threads |
871 |
|
*/ |
872 |
|
public int getActiveThreadCount() { |
873 |
|
return activeCountOf(runControl); |
877 |
|
* Returns an estimate of the number of threads that are currently |
878 |
|
* idle waiting for tasks. This method may underestimate the |
879 |
|
* number of idle threads. |
880 |
< |
* @return the number of idle threads. |
880 |
> |
* @return the number of idle threads |
881 |
|
*/ |
882 |
|
final int getIdleThreadCount() { |
883 |
|
int c = runningCountOf(workerCounts) - activeCountOf(runControl); |
906 |
|
* tuning fork/join programs: In general, steal counts should be |
907 |
|
* high enough to keep threads busy, but low enough to avoid |
908 |
|
* overhead and contention across threads. |
909 |
< |
* @return the number of steals. |
909 |
> |
* @return the number of steals |
910 |
|
*/ |
911 |
|
public long getStealCount() { |
912 |
|
return stealCount.get(); |
929 |
|
* an approximation, obtained by iterating across all threads in |
930 |
|
* the pool. This method may be useful for tuning task |
931 |
|
* granularities. |
932 |
< |
* @return the number of queued tasks. |
932 |
> |
* @return the number of queued tasks |
933 |
|
*/ |
934 |
|
public long getQueuedTaskCount() { |
935 |
|
long count = 0; |
948 |
|
* Returns an estimate of the number tasks submitted to this pool |
949 |
|
* that have not yet begun executing. This method takes time |
950 |
|
* proportional to the number of submissions. |
951 |
< |
* @return the number of queued submissions. |
951 |
> |
* @return the number of queued submissions |
952 |
|
*/ |
953 |
|
public int getQueuedSubmissionCount() { |
954 |
|
return submissionQueue.size(); |
957 |
|
/** |
958 |
|
* Returns true if there are any tasks submitted to this pool |
959 |
|
* that have not yet begun executing. |
960 |
< |
* @return {@code true} if there are any queued submissions. |
960 |
> |
* @return {@code true} if there are any queued submissions |
961 |
|
*/ |
962 |
|
public boolean hasQueuedSubmissions() { |
963 |
|
return !submissionQueue.isEmpty(); |
1721 |
|
* Possibly blocks the current thread, for example waiting for |
1722 |
|
* a lock or condition. |
1723 |
|
* @return true if no additional blocking is necessary (i.e., |
1724 |
< |
* if isReleasable would return true). |
1724 |
> |
* if isReleasable would return true) |
1725 |
|
* @throws InterruptedException if interrupted while waiting |
1726 |
|
* (the method is not required to do so, but is allowed to). |
1727 |
|
*/ |
1760 |
|
* attempt to maintain the pool's nominal parallelism; otherwise |
1761 |
|
* activate a thread only if necessary to avoid complete |
1762 |
|
* starvation. |
1763 |
< |
* @throws InterruptedException if blocker.block did so. |
1763 |
> |
* @throws InterruptedException if blocker.block did so |
1764 |
|
*/ |
1765 |
|
public static void managedBlock(ManagedBlocker blocker, |
1766 |
|
boolean maintainParallelism) |
1822 |
|
|
1823 |
|
private static long fieldOffset(String fieldName) |
1824 |
|
throws NoSuchFieldException { |
1825 |
< |
return _unsafe.objectFieldOffset |
1825 |
> |
return UNSAFE.objectFieldOffset |
1826 |
|
(ForkJoinPool.class.getDeclaredField(fieldName)); |
1827 |
|
} |
1828 |
|
|
1829 |
< |
static final Unsafe _unsafe; |
1829 |
> |
static final Unsafe UNSAFE; |
1830 |
|
static final long eventCountOffset; |
1831 |
|
static final long workerCountsOffset; |
1832 |
|
static final long runControlOffset; |
1835 |
|
|
1836 |
|
static { |
1837 |
|
try { |
1838 |
< |
_unsafe = getUnsafe(); |
1838 |
> |
UNSAFE = getUnsafe(); |
1839 |
|
eventCountOffset = fieldOffset("eventCount"); |
1840 |
|
workerCountsOffset = fieldOffset("workerCounts"); |
1841 |
|
runControlOffset = fieldOffset("runControl"); |
1847 |
|
} |
1848 |
|
|
1849 |
|
private boolean casEventCount(long cmp, long val) { |
1850 |
< |
return _unsafe.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1850 |
> |
return UNSAFE.compareAndSwapLong(this, eventCountOffset, cmp, val); |
1851 |
|
} |
1852 |
|
private boolean casWorkerCounts(int cmp, int val) { |
1853 |
< |
return _unsafe.compareAndSwapInt(this, workerCountsOffset, cmp, val); |
1853 |
> |
return UNSAFE.compareAndSwapInt(this, workerCountsOffset, cmp, val); |
1854 |
|
} |
1855 |
|
private boolean casRunControl(int cmp, int val) { |
1856 |
< |
return _unsafe.compareAndSwapInt(this, runControlOffset, cmp, val); |
1856 |
> |
return UNSAFE.compareAndSwapInt(this, runControlOffset, cmp, val); |
1857 |
|
} |
1858 |
|
private boolean casSpareStack(WaitQueueNode cmp, WaitQueueNode val) { |
1859 |
< |
return _unsafe.compareAndSwapObject(this, spareStackOffset, cmp, val); |
1859 |
> |
return UNSAFE.compareAndSwapObject(this, spareStackOffset, cmp, val); |
1860 |
|
} |
1861 |
|
private boolean casBarrierStack(WaitQueueNode cmp, WaitQueueNode val) { |
1862 |
< |
return _unsafe.compareAndSwapObject(this, syncStackOffset, cmp, val); |
1862 |
> |
return UNSAFE.compareAndSwapObject(this, syncStackOffset, cmp, val); |
1863 |
|
} |
1864 |
|
} |