gpt4 book ai didi

java - 为什么多线程在这个程序中不起作用?

转载 作者:行者123 更新时间:2023-11-29 05:09:22 26 4
gpt4 key购买 nike

我编写了一个程序,用于查找 1 到 100,000 范围内具有最大除数的数:

 /**
A program that finds the number with the largest number of divisors in the range of 1 to 100,000. This version simply uses threads.
*/

public class ThreadDivisors{

private static final int SAMPLE_SIZE = 100000;
private static final int THREAD_COUNT = 2;
private volatile static int maxDividend;
private volatile static int maxDivisorCount = 0;

public static void main(String[] args){

int start = 1;
int end = SAMPLE_SIZE / THREAD_COUNT;
DivisorThread[] threads = new DivisorThread[THREAD_COUNT];

for(int i = 0; i < THREAD_COUNT; i++){
threads[i] = new DivisorThread(start, end);
System.out.println("Thread created starting at " + start + " and ending at " + end);//debug
start += SAMPLE_SIZE / THREAD_COUNT;
end += SAMPLE_SIZE / THREAD_COUNT;
}

for(int i = 0; i < THREAD_COUNT; i++){
threads[i].start();
}

for(int i = 0; i < THREAD_COUNT; i++){
while(threads[i].isAlive()){
try{
threads[i].join();
}
catch(InterruptedException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

System.out.println("The number with the most divisors is " + maxDividend);
System.out.println(maxDivisorCount);

}

static class DivisorThread extends Thread{

static int start;
static int end;

public DivisorThread(int start, int end){
this.start = start;
this.end = end;
}

public void run(){

System.out.println("Thread running...");
divisorCount(start, end);

}

}

private static void divisorCount(int start, int end){

int divisorCount;//number of divisors for number being divided
int maxThreadDividend = start;
int maxThreadDivisorCount = 0;

for (int dividend = start; dividend <= end; dividend++){//iterate through dividends

divisorCount = 0;

for (int divisor = start; divisor <= dividend; divisor++){//iterate through divisors

if (dividend % divisor == 0){
divisorCount++;
}//end if

}//end for

if (divisorCount > maxThreadDivisorCount){
maxThreadDivisorCount = divisorCount;
maxThreadDividend = dividend;
System.out.println(maxThreadDividend);
System.out.println(maxThreadDivisorCount);
}

}//end for

report(maxThreadDivisorCount, maxThreadDividend);

}
/*This subroutine updates global variables that keep track of which number has the most divisors.*/
private synchronized static void report(int maxThreadDivisorCount, int maxThreadDividend){

if(maxThreadDivisorCount > maxDivisorCount){
maxDivisorCount = maxThreadDivisorCount;
maxDividend = maxThreadDividend;
}

}

}

它适用于一个线程(全局变量 THREAD_COUNT 决定这一点),但不适用于更多线程。例如,当我将 THREAD_COUNT 设置为 2 时,我得到以下输出:

创建的线程从 1 开始到 50000 结束创建的线程从 50001 开始到 100000 结束线程运行...500011个线程运行...500011个除数最多的数是500011

子例程 divisorCount() 似乎只是在具有最大起点的任何线程的起点处停止……它不会继续前进。是什么导致了这个问题?

最佳答案

问题是您的内部 for 循环。您的除数不能从 start 开始,而是从 1(或 2,具体取决于您是从 0 还是 1(甚至 2,以后更多)开始除数计数器)

这应该可以解决您的问题。

在一个不相关的说明中,我认为您可以从 maxDivisormaxDividend 变量中删除 volatile 关键字,因为对它们的所有访问都在同步块(synchronized block)中。

还有一点。除数不会超过检查股息的一半(除了数字本身)相应地调整您的限制应该会给您带来显着的性能提升:

for (int dividend = start; dividend <= end; dividend++){

divisorCount = 2;// 1 and dividend are always divisors

for (int divisor = 2; divisor <= dividend / 2; divisor++){

关于java - 为什么多线程在这个程序中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29225962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com