就是安卓java文件里如何调用下面类,算阶乘 public final class Factorial {/** * Default thread count(CPU count * 4) */ static final int THREAD_COUNT; static { THREAD_COUNT = Runtime.getRuntime().availableProcessors() << 2; }/** * Never instantiate this class. */ private Factorial() { }/** * Calculates the factorial of n. A new thread pool will be initialized to * perform the calculation task, and destroyed thereafter. * * @param n * the value of n * @return the factorial of the given n * @throws Exception * if exception occurs */ public static final BigInteger factorial(final int n) throws Exception { ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT); try { return factorial(n, threadPool, THREAD_COUNT); } finally { threadPool.shutdown(); } }/** * Calculates the factorial of n. A specified thread pool will be used to * perform the calculation task, and will not be automatically destroyed. * * @param n * the value of n * @param threadPool * the given thread pool to perform the calculation task * @param threadCount * the number of threads to perform the calculation task * @return the factorial of the given n * @throws Exception * if exception occurs */ public static BigInteger factorial(final int n, ExecutorService threadPool, final int threadCount) throws Exception {List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>( threadCount); for (int i = 1; i <= threadCount; i++) { final int threadNo = i; Callable<BigInteger> worker = new Callable<BigInteger>() {@Override public BigInteger call() throws Exception { BigInteger result = BigInteger.ONE; for (int x = threadNo; x <= n; x += threadCount) { result = BigInteger.valueOf(x).multiply(result); } return result; }}; tasks.add(worker); }BigInteger result = BigInteger.ONE; List<Future<BigInteger>> futureList = threadPool.invokeAll(tasks); for (Future<BigInteger> future : futureList) { result = future.get().multiply(result); } return result; } }
类看了教程还是迷糊
public final class Factorial {/**
* Default thread count(CPU count * 4)
*/
static final int THREAD_COUNT;
static {
THREAD_COUNT = Runtime.getRuntime().availableProcessors() << 2;
}/**
* Never instantiate this class.
*/
private Factorial() {
}/**
* Calculates the factorial of n. A new thread pool will be initialized to
* perform the calculation task, and destroyed thereafter.
*
* @param n
* the value of n
* @return the factorial of the given n
* @throws Exception
* if exception occurs
*/
public static final BigInteger factorial(final int n) throws Exception {
ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
try {
return factorial(n, threadPool, THREAD_COUNT);
} finally {
threadPool.shutdown();
}
}/**
* Calculates the factorial of n. A specified thread pool will be used to
* perform the calculation task, and will not be automatically destroyed.
*
* @param n
* the value of n
* @param threadPool
* the given thread pool to perform the calculation task
* @param threadCount
* the number of threads to perform the calculation task
* @return the factorial of the given n
* @throws Exception
* if exception occurs
*/
public static BigInteger factorial(final int n, ExecutorService threadPool,
final int threadCount) throws Exception {List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(
threadCount);
for (int i = 1; i <= threadCount; i++) {
final int threadNo = i;
Callable<BigInteger> worker = new Callable<BigInteger>() {@Override
public BigInteger call() throws Exception {
BigInteger result = BigInteger.ONE;
for (int x = threadNo; x <= n; x += threadCount) {
result = BigInteger.valueOf(x).multiply(result);
}
return result;
}};
tasks.add(worker);
}BigInteger result = BigInteger.ONE;
List<Future<BigInteger>> futureList = threadPool.invokeAll(tasks);
for (Future<BigInteger> future : futureList) {
result = future.get().multiply(result);
}
return result;
}
}