我编写了一个程序,可以找到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 线程运行... 50001 1 线程运行... 50001 1 除数最多的数字是50001 1
子程序divisorCount()似乎只是以任何线程具有最大起点的起点停止......它不会更进一步。可能导致这个问题的原因是什么?
答案 0 :(得分:3)
问题是你内心的循环。你的除数不能从start
开始,而是从1开始(或者2,取决于你用0或1开始除数计数器(或者甚至更晚2))
那应该可以解决你的问题。
在一个不相关的说明中,我认为您可以从maxDivisor
和maxDividend
变量中删除volatile关键字,因为对它们的所有访问都在同步块中。
另一件事。将不会有超过已核算红利一半的除数(除了数字本身)相应地调整您的限制应该会给您带来显着的提升:
for (int dividend = start; dividend <= end; dividend++){
divisorCount = 2;// 1 and dividend are always divisors
for (int divisor = 2; divisor <= dividend / 2; divisor++){