/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.internal.util;

import static java.lang.Math.max;

import java.util.ArrayList;
import java.util.List;
import java.util.RandomAccess;
import java.util.concurrent.RecursiveAction;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version 2.0
 * @since 2.0
 */
final class RunnablesAction extends RecursiveAction {
	private static final long serialVersionUID = 1;

	private static final int DEFAULT_THRESHOLD = 7;

	private final List<? extends Runnable> _runnables;
	private final int _high;
	private final int _low;
	private final Integer _threshold;

	private RunnablesAction(
		final List<? extends Runnable> runnables,
		final int low,
		final int high,
		final Integer threshold
	) {
		_runnables = runnables;
		_low = low;
		_high = high;
		_threshold = threshold;
	}

	public RunnablesAction(final List<? extends Runnable> runnables) {
		this(
			runnables instanceof RandomAccess ?
				runnables :
				new ArrayList<>(runnables),
			0,
			runnables.size(),
			null
		);
	}

	@Override
	protected void compute() {
		final int threshold = _threshold != null ? _threshold : threshold();

		if ((_high - _low) <= 1 || getSurplusQueuedTaskCount() > 3) {
		//if (_high - _low < threshold) {
			for (int i = _low; i < _high; ++i) {
				_runnables.get(i).run();
			}
		} else {
			final int mid = (_low + _high) >>> 1;
			invokeAll(
				new RunnablesAction(_runnables, _low, mid, threshold),
				new RunnablesAction(_runnables, mid, _high, threshold)
			);
		}
	}

	private int threshold() {
		return max(_runnables.size()/(Concurrency.CORES*2), DEFAULT_THRESHOLD);
	}

}

@@ -805,16 +805,114 @@
 th.max;%0A
+import static java.security.AccessController.doPrivileged;%0A%0Aimport java.security.PrivilegedAction;
 %0Aimport 
@@ -1130,19 +1130,29 @@
 version 
-2.0
+!__version__!
 %0A * @sin
@@ -1270,58 +1270,8 @@
 1;%0A%0A
-%09private static final int DEFAULT_THRESHOLD = 7;%0A%0A
 %09pri
@@ -1372,43 +1372,8 @@
 low;
-%0A%09private final Integer _threshold;
 %0A%0A%09p
@@ -1477,35 +1477,8 @@
 high
-,%0A%09%09final Integer threshold
 %0A%09) 
@@ -1540,45 +1540,12 @@
 h;%0A%09
-%09_threshold = threshold;%0A%09%7D%0A%0A%09public 
+%7D%0A%0A%09
 Runn
@@ -1648,15 +1648,15 @@
 cess
- ?
 %0A%09%09%09%09
+? 
 runn
@@ -1664,15 +1664,15 @@
 bles
- :
 %0A%09%09%09%09
+: 
 new 
@@ -1724,17 +1724,8 @@
 ze()
-,%0A%09%09%09null
 %0A%09%09)
@@ -1775,107 +1775,57 @@
 %7B%0A%09%09
-final int threshold = _threshold != null ? _threshold : threshold();%0A%0A%09%09if ((_high - _low) %3C= 1 %7C%7C 
+if ((_high - _low) %3C= SplitThreshold.VALUE %7C%7C%0A%09%09%09
 getS
@@ -1856,45 +1856,11 @@
 %3E 3)
- %7B
 %0A%09%09
-//if (_high - _low %3C threshold) 
 %7B%0A%09%09
@@ -2042,27 +2042,16 @@
 low, mid
-, threshold
 ),%0A%09%09%09%09n
@@ -2095,19 +2095,8 @@
 high
-, threshold
 )%0A%09%09
@@ -2120,21 +2120,41 @@
 ate 
-int t
+static final class SplitT
 hreshold
 () %7B
@@ -2153,84 +2153,194 @@
 hold
-()
  %7B%0A%09%09
-return max(_runnables.size()/(Concurrency.CORES*2), DEFAULT_THRESHOLD
+private static final int VALUE = max(%0A%09%09%09doPrivileged(%0A%09%09%09%09(PrivilegedAction%3CInteger%3E)() -%3E Integer.getInteger(%0A%09%09%09%09%09%22io.jenetics.concurrency.splitThreshold%22,%0A%09%09%09%09%091%0A%09%09%09%09)),%0A%09%09%091%0A%09%09
 );%0A%09

Make the split-threshold configurable.