Java 为什么多个线程在此程序中不工作?
我编写了一个程序,可以找到除数在1到100000之间的最大数:Java 为什么多个线程在此程序中不工作?,java,multithreading,Java,Multithreading,我编写了一个程序,可以找到除数在1到100000之间的最大数: /** 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 SA
/**
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;
}
}
}
/**
查找除数在1到100000之间的最大数的程序。这个版本只使用线程。
*/
公共类除数{
私有静态最终整数样本大小=100000;
私有静态最终int线程计数=2;
私有易失静态整数最大红利;
私有易失性静态int-maxDivisorCount=0;
公共静态void main(字符串[]args){
int start=1;
int end=样本大小/螺纹计数;
DivisorThread[]线程=新的DivisorThread[THREAD_COUNT];
对于(int i=0;i
它只适用于一个线程(全局变量thread_COUNT决定了这一点),但不适用于多个线程。例如,当我将THREAD_COUNT设置为2时,我得到以下输出:
从1开始到50000结束创建的线程
从50001开始到100000结束创建的线程
线程正在运行。。。
50001
1.
线程正在运行。。。
50001
1.
除数最多的数字是50001
一,
子例程divisorCount()似乎只是以任何具有最大起点的线程的起点停止……它不会进一步。什么可能导致此问题?问题在于内部for循环。除数不能从
start
开始,而是从1开始(或2,取决于您是从0还是1开始除数计数器(甚至2,更晚))
这会解决你的问题
另一方面,我认为您可以从maxdivisior
和maxdivision
变量中删除volatile关键字,因为它们的所有访问都在一个同步块中
还有一件事。除数不会超过支票红利的一半(除数字本身外),因此调整您的限额应该会给您带来显著的性能提升:
for (int dividend = start; dividend <= end; dividend++){
divisorCount = 2;// 1 and dividend are always divisors
for (int divisor = 2; divisor <= dividend / 2; divisor++){
for(int divident=start;divident No)“从1开始创建的线程”?是的,它确实这么说。