gpt4 book ai didi

java - 为什么执行相同行为的线程需要不同的时间?

转载 作者:行者123 更新时间:2023-12-02 06:29:28 24 4
gpt4 key购买 nike

我是多线程新手。我有一个由两个类组成的程序:PrimeNumber 和一个主类。我试图找到给定范围内的所有素数。这是我的 PrimeNumber 类:

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;


public class PrimeNumber extends Thread{

int start, end;
int threadNumber; //used to display the thread number
static ArrayList<Integer> list = new ArrayList<Integer>(1000000);


public PrimeNumber(int start, int end, int threadNumber) {
this.start = start;
this.end = end;
this.threadNumber = threadNumber;
// added code
if(list.isEmpty()){
list.add(2);
}


}
@Override
public void run(){
long startTime = System.nanoTime();
System.out.println(threadNumber + " started");
for(int i = start; i<=end;i++){
if(isPrime(i)){
list.add(i);
}

}
System.out.println(threadNumber + " has finished");
long endTime = System.nanoTime();

System.out.println("Time for thread " + threadNumber + " is " +TimeUnit.SECONDS.convert(endTime-startTime, TimeUnit.NANOSECONDS) + " seconds.");

}

//modified method
/**
* Determine whether a number is prime
* @param number
* @return true if number is prime, false otherwise
*/


public boolean isPrime(int number){
if(number == 0 || number == 1){
return false;
}

else {
int counter = 0;
while(counter<list.size()){
if(number%list.get(counter)==0){
return false;
}
counter++;
}
}

return true;
}
}

这是我的主课

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class MainClass {

public static void main(String[] args) throws InterruptedException{
final int maxNumberOfThreads = 3; // number of threads I want to create
final int maxNumber = 900000; // Max range for which I'm finding all the prime numbers up to it
int initialNumber = 1;
ArrayList<Thread> myList = new ArrayList<Thread>();
for(int i = 0; i < maxNumberOfThreads; i++ ){
myList.add(new PrimeNumber(initialNumber, initialNumber+ maxNumber/maxNumberOfThreads, i+1));
myList.get(i).start();
initialNumber+=maxNumber/maxNumberOfThreads;
}

for(Thread thread : myList){

thread.join();
}
try {
Collections.sort(PrimeNumber.list); // sort the list
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("Primes.txt"),true));
for(int i = 0; i <PrimeNumber.list.size(); i++){
writer.write(PrimeNumber.list.get(i).toString());
//System.out.println(PrimeNumber.list.get(i));
writer.newLine();
}
writer.close();
System.out.println("Done writing to the file");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

当我运行我的程序时,这是我得到的输出:

2 started
3 started
1 started
1 has finished
Time for thread 1 is 3 seconds.
2 has finished
Time for thread 2 is 7 seconds.
3 has finished
Time for thread 3 is 11 seconds.
Done writing to the file

尽管线程具有相似的行为(它们都在几乎相同的范围内计算素数),但为什么每个线程的执行时间不同?我已经搜索了很长一段时间但没有找到满意的答案。提前致谢。

编辑:我在 isPrime() 方法中添加了一个步骤,大大缩短了执行时间。

最佳答案

方法 isPrime 中的此循环在线程 3 上执行的迭代次数多于线程 2,在线程 2 上执行的迭代次数多于线程 1,因为每种情况下测试的数字都不同:

     for(int i = 2; i< number; i++)
{
if(number%i ==0){
return false;
}
}
return true;

而且,三个范围内的素数个数不同,所以添加到列表中的个数也会不同。

关于java - 为什么执行相同行为的线程需要不同的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20209145/

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