gpt4 book ai didi

Java线程操作,并行编程

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

我正在参加有关 Java 编程的在线类(class)并遇到了这个问题,我正在研究使用线程的并行编程。

我要制作一个“四核”版本的程序,通过将总和的范围分成四等份来计算 pi,并使用四个线程。

我试图将它分成 4 个不同的线程并启动和加入每个线程。

public class quadCorePi extends Thread {

public static void main(String[] args) throws Exception {

long startTime = System.currentTimeMillis();

quadCorePi thread1 = new quadCorePi();
thread1.begin = 0 ;
thread1.end = numSteps / 4 ;

quadCorePi thread2 = new quadCorePi();
thread2.begin = 1 ;
thread2.end = numSteps / 4 ;

quadCorePi thread3 = new quadCorePi();
thread3.begin = 2 ;
thread3.end = numSteps / 4 ;

quadCorePi thread4 = new quadCorePi();
thread4.begin = numSteps / 4 ;
thread4.end = numSteps ;



thread1.start();
thread2.start();
thread3.start();
thread4.start();

thread1.join();
thread2.join();
thread3.join();
thread4.join();

long endTime = System.currentTimeMillis();

double pi = step * (thread1.sum + thread2.sum + thread3.sum + thread4.sum);


System.out.println("Value of pi: " + pi);

System.out.println("Calculated in " +
(endTime - startTime) + " milliseconds");
}

但它给了我错误的 pi 值,解释对于如何为线程拆分工作非常有帮助。

这是给出的示例代码:

public class ParallelPi extends Thread {

public static void main(String[] args) throws Exception {

long startTime = System.currentTimeMillis();

ParallelPi thread1 = new ParallelPi();
thread1.begin = 0 ;
thread1.end = numSteps / 2 ;

ParallelPi thread2 = new ParallelPi();
thread2.begin = numSteps / 2 ;
thread2.end = numSteps ;

thread1.start();
thread2.start();

thread1.join();
thread2.join();

long endTime = System.currentTimeMillis();

double pi = step * (thread1.sum + thread2.sum) ;

System.out.println("Value of pi: " + pi);

System.out.println("Calculated in " +
(endTime - startTime) + " milliseconds");
}

static int numSteps = 10000000;

static double step = 1.0 / (double) numSteps;

double sum ;
int begin, end ;

public void run() {

sum = 0.0 ;

for(int i = begin ; i < end ; i++){
double x = (i + 0.5) * step ;
sum += 4.0 / (1.0 + x * x);
}
}
}

最佳答案

您的限制不正确。这是更正后的代码:

public class QuadCorePi extends Thread {

public static void main(String[] args) throws Exception {

long startTime = System.currentTimeMillis();

QuadCorePi thread1 = new QuadCorePi();
thread1.begin = 0 ;
thread1.end = numSteps / 4 ;

QuadCorePi thread2 = new QuadCorePi();
thread2.begin = numSteps / 4 ;
thread2.end = numSteps / 2 ;

QuadCorePi thread3 = new QuadCorePi();
thread3.begin = numSteps / 2 ;
thread3.end = 3 * numSteps / 4 ;

QuadCorePi thread4 = new QuadCorePi();
thread4.begin = 3 * numSteps / 4 ;
thread4.end = numSteps ;

thread1.start();
thread2.start();
thread3.start();
thread4.start();

thread1.join();
thread2.join();
thread3.join();
thread4.join();

long endTime = System.currentTimeMillis();

double pi = step * (thread1.sum + thread2.sum + thread3.sum + thread4.sum) ;

System.out.println("Value of pi: " + pi);

System.out.println("Calculated in " +
(endTime - startTime) + " milliseconds");
}

static int numSteps = 10000000;

static double step = 1.0 / (double) numSteps;

double sum ;
int begin, end ;

public void run() {

sum = 0.0 ;

for(int i = begin ; i < end ; i++){
double x = (i + 0.5) * step ;
sum += 4.0 / (1.0 + x * x);
}
}
}

关于Java线程操作,并行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29297898/

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