多线程计算π

发布于:2024-10-13 ⋅ 阅读:(135) ⋅ 点赞:(0)

1、实现单线程计算π

2、使用任务分解方法,使用2线程,并行计算π

3、使用数据分解方法,使用2线程,并行计算π

注意:在循环中使用以上计算π的公式,n取值为1到Int.Max

问题1:

import java.lang.*;

public class Thread1 extends Thread{
    @Override
    public void run(){
        long startime = System.currentTimeMillis();
        double pi = 0;
        double i = 1.0,s = 1.0;
        double n = 1.0;
        int sum = 0;
        while(Math.abs(i) >= 1e-9){
            pi += i;
            n += 2;
            s = -s;
            i = s / n;
            sum++;
        }
        System.out.println(pi*4);
        System.out.println(sum);
        long endtime = System.currentTimeMillis();
        System.out.println("单线程耗时:"+ (endtime-startime) +" ms");
    }
}
public class Main {
    public static void main(String[] args) {
        Thread1 t1 = new Thread1();
        t1.start();
    }
}

问题2:

import java.lang.*;

public class Thread1 extends Thread{
    protected double pi = 0;
    protected double i,s;
    protected double n;

    @Override
    public void run(){
        while(Math.abs(i) >= 1e-9){
            pi += i;
            n += 4;
            i = s / n;
        }
    }

    public double getPi(){
        return pi;
    }

    public void setI(double i) {
        this.i = i;
    }

    public void setN(double n) {
        this.n = n;
    }

    public void setS(double s) {
        this.s = s;
    }
}
public class Main {
    public static void main(String[] args) {
        int threadNum = 2;
        Thread1[] threads = new Thread1[threadNum];
        for (int i = 0; i < threadNum; i++) {
            threads[i] = new Thread1();
            double s = 0;
            if (i % 2 == 0) {
                s = 1.0;
                threads[i].setS(s);
                threads[i].setI(1.0);
                threads[i].setN(1.0);
            }else{
                s = -1.0;
                threads[i].setS(s);
                threads[i].setI(1.0/-3.0);
                threads[i].setN(3.0);
            }
        }
        double res = 0;
        System.out.println("采用任务分解法进行双线程计算");
        long startime = System.currentTimeMillis();
        for (int i = 0; i < threadNum; i++) {
            threads[i].start();
        }
        try{
            for (int i = 0; i < threadNum; i++) {
                threads[i].join();
                res += threads[i].getPi();
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(res*4);
        long endtime = System.currentTimeMillis();
        System.out.println("双线程耗时:"+ (endtime-startime) +" ms");
    }
}

问题3:

import java.lang.*;

public class Thread1 extends Thread{
    protected double pi = 0;
    protected double i,s = 1.0;
    protected double n;
    protected double end;

    @Override
    public void run(){
        while(n < end){
            pi += i;
            n += 2;
            s = -s;
            i = s / n;
        }
    }

    public double getPi(){
        return pi;
    }

    public void setI(double i) {
        this.i = i;
    }

    public void setEnd(double end) {
        this.end = end;
    }

    public void setN(double n) {
        this.n = n;
    }

    public void setS(double s) {
        this.s = s;
    }
}
public class Main {
    public static void main(String[] args) {
        int threadNum = 2;
        Thread1[] threads = new Thread1[threadNum];
        for (int i = 0; i < threadNum; i++) {
            threads[i] = new Thread1();
            if (i % 2 == 0) {
                threads[i].setI(1.0);
                threads[i].setN(1.0);
                threads[i].setEnd(500000001.0);
            }else{
                threads[i].setI(1.0/500000001.0);
                threads[i].setN(500000001.0);
                threads[i].setEnd(1000000001.0);
            }
        }
        double res = 0;
        System.out.println("采用数据分解法进行双线程计算:");
        long startime = System.currentTimeMillis();
        for (int i = 0; i < threadNum; i++) {
            threads[i].start();
        }
        try{
            for (int i = 0; i < threadNum; i++) {
                threads[i].join();
                res += threads[i].getPi();
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(res*4);
        long endtime = System.currentTimeMillis();
        System.out.println("双线程耗时:"+ (endtime-startime) +" ms");
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到