Java 使用线程计算给定数组中的完美正方形数
我试图使用线程计算给定数组中的完美正方形数。 每个线程都应该从数组中选取一部分,并在该部分中搜索完美正方形的数量。然后,将该部分中的正方形数量添加到整个阵列中完美正方形的全局数量中 下面是我的课程Java 使用线程计算给定数组中的完美正方形数,java,arrays,multithreading,Java,Arrays,Multithreading,我试图使用线程计算给定数组中的完美正方形数。 每个线程都应该从数组中选取一部分,并在该部分中搜索完美正方形的数量。然后,将该部分中的正方形数量添加到整个阵列中完美正方形的全局数量中 下面是我的课程 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class SquareCounter
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class SquareCounterImpl {
public static int count = 0;
public static int div;
public static long t[];
public int countSquares(long[] numbers, int nThreads)
{
//count = nThreads;
t = numbers;
div = numbers.length/nThreads;
ExecutorService exec = Executors.newFixedThreadPool(nThreads);
for (int i = 0; i < nThreads; i++) {
exec.execute(new MeinRunnable(i));
try
{exec.wait();}catch(Exception e){}
}
return count;
}
static boolean isPerfectSquare(long n)
{
int i =1;
while(true)
{
if(n < 0)
return false;
if(n == 0 )
return true;
n-=i;
i+=2;
}
}
protected static class MeinRunnable extends Thread {
final int n;
public MeinRunnable(int i) {
n = i;
}
synchronized void incrementSync() {
count = count + 1;
}
public void run() {
//System.out.println((div*n )+ "-" + (div*(n+1)) );
for(int i=div*n ; i < div*(n+1); i++)
{
if(isPerfectSquare(t[i]));
incrementSync();
}
}
}
}
似乎我的计数总是停留在5(数组第一次除法中的完美正方形数)。谁能告诉我我做错了什么。谢谢。在输出
计数之前,您不会等待线程完成。您需要的不是wait()
,而是:
exec.shutdown();
try {
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
...
}
另外,我认为您有一个bug:
if(isPerfectSquare(t[i]));
incrementSync();
应该是
if(isPerfectSquare(t[i])) {
incrementSync();
}
我还将使用AtomicInteger而不是静态int。建议更易于并行
public static void main(String[] args) {
long start = System.nanoTime();
try (LongStream longs = LongStream.range(1, 3_900_000)) {
long count = longs.parallel().filter(n -> isPerfectSquare(n)).count();
System.out.println((System.nanoTime() - start) / 1_000_000);
System.out.println(count);
}
}
private static boolean isPerfectSquare(long n) {
int i = 1;
while (true) {
if (n < 0) {
return false;
}
if (n == 0) {
return true;
}
n -= i;
i += 2;
}
}
publicstaticvoidmain(字符串[]args){
长启动=System.nanoTime();
try(LongStream longs=LongStream.range(1,3_900_000)){
long count=longs.parallel().filter(n->isPerfectSquare(n)).count();
System.out.println((System.nanoTime()-start)/1_000_000);
系统输出打印项次(计数);
}
}
专用静态布尔值isPerfectSquare(长n){
int i=1;
while(true){
if(n<0){
返回false;
}
如果(n==0){
返回true;
}
n-=i;
i+=2;
}
}
请不要将您的类命名为“Impl”。我很生气。正确答案应该是什么?@SteveSmith正确答案应该是10,因为我有两条线。每个线程将从数组中取出5个元素的除法,因为每个元素都是一个完美的正方形,所以第一个线程应该返回5,第二个线程应该返回5。我修好了程序错误,所以谢谢。我仍然只得到5个完美的方块。你仍然没有等待线程完成。请参阅我的编辑。实施修复后,我现在得到一个java.util.concurrent.RejectedExecutionException.exec.shutdown()是导致修复异常的原因。非常感谢。额外的代码需要添加到循环之外。如果这个答案有用的话,别忘了标记它。:)
public static void main(String[] args) {
long start = System.nanoTime();
try (LongStream longs = LongStream.range(1, 3_900_000)) {
long count = longs.parallel().filter(n -> isPerfectSquare(n)).count();
System.out.println((System.nanoTime() - start) / 1_000_000);
System.out.println(count);
}
}
private static boolean isPerfectSquare(long n) {
int i = 1;
while (true) {
if (n < 0) {
return false;
}
if (n == 0) {
return true;
}
n -= i;
i += 2;
}
}