50 lines
1.1 KiB
Java
50 lines
1.1 KiB
Java
package parallel;
|
|
|
|
import util.Prime;
|
|
import java.util.List;
|
|
import util.PrimeCounter;
|
|
import java.util.ArrayList;
|
|
import java.util.concurrent.Future;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
public class ThreadPoolPrimeCounter implements PrimeCounter {
|
|
|
|
private final int numThreads;
|
|
|
|
public ThreadPoolPrimeCounter(int numThreads) {
|
|
this.numThreads = numThreads;
|
|
}
|
|
|
|
public int countPrimes(int[] nums) throws Exception {
|
|
int count = 0;
|
|
// TODO: Complete method countPrimes
|
|
|
|
return count;
|
|
}
|
|
|
|
private static class CountPrimes implements Callable<Integer> {
|
|
private final int[] nums;
|
|
private final int low, high;
|
|
|
|
private CountPrimes(int[] nums, int low, int high) {
|
|
this.nums = nums;
|
|
this.low = low;
|
|
this.high = Math.min(high, nums.length);
|
|
}
|
|
|
|
@Override
|
|
public Integer call() throws Exception {
|
|
int count = 0;
|
|
for (int i = low; i < high; i++) {
|
|
if (Prime.isPrime(nums[i])) {
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
}
|
|
}
|