多线程和停止线程

Multi-threading and stopping a thread

我的项目真的需要一些帮助。 任务 : 测试的目的是使用各种方法创建 π (Pi) 计算 由多线程加速的计算过程。 使用 BigDecimal class 以获得更高的精度。 使用您自己的异常 classes 并将所有 classes 打包成一个 整洁的包装概念。

我尝试实现 Leibniz 方法,但我的主要问题是当线程处于 运行 时,我不知道如何从我的主要方法中停止线程。我的老师向我们展示了他的 mian 方法的示例,您可以清楚地看到他正在使用例如 4 个线程启动该方法。几秒钟后,他能够停止所有线程。 下面是他的主要例子 class:

        CalculatePi pi = new Leibniz();

    System.out.println("Start: " + pi.getMethodName());
    pi.startCalculation(4); //four threads

    int prec = 0;
    BigDecimal result = BigDecimal.ZERO;
    long timeStart = System.currentTimeMillis();
    while(prec < MAX_PRECISION) {
        someDelay(); //give some time to calculate
        BigDecimal newResult = pi.getValue();
        int newPrec = precicion(result, newResult);
        if(newPrec != prec) {
            System.out.println("pi (" + newPrec + "): " + newResult);
            prec = newPrec;
        }
        result = newResult;
    }
    long timeStop = System.currentTimeMillis();
    pi.stopCalculation();
    System.out.println( (timeStop - timeStart) + " ms");
    System.out.println(pi.getInternalSteps() + " calulation steps");

这是我实现任务的第一个想法(不要混淆我主要关注方法 "startCalculation(int numThreads)" 和 "stopCalculation()",它们都是由接口给出的)

    // Methode soll Leibniz Verfahren mit mehreren Threads berechnen
@Override
public boolean startCalculation(int numThreads) {
    // Threads müssen in Array gespeichert werden um damit zu arbeiten
    LeibnizThread[] threadSpeicher = new LeibnizThread[numThreads];

    for(int i = 0; i < numThreads; i++) {
        // Neuen Thread initialisieren und im Array speichern
        threadSpeicher[i] = new LeibnizThread(numThreads, i);
        //Thread starten
        threadSpeicher[i].start();
    }
    //Warten bis alle Threads fertig sind und ihr ergebnis berechnet haben
    for(LeibnizThread w : threadSpeicher)
        try {
            w.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    BigDecimal sum = new BigDecimal(0.0);
    //Summe aller Threads zusammenrechnen
    for(LeibnizThread w : threadSpeicher) {
        System.out.println(w.getResult() + " Zwischenergebnis");
        sum = sum.add(w.getResult());
    }
    //Summe wird mit 4 multipliziert, um finales Ergebnis zu erhalten
    this.value = sum.multiply(new BigDecimal(4));
    System.out.println("Ergebnis: " + this.value);

    return true;
}
//Methode soll Threads aus Methode startCalculation(numThreads) stoppen, aber wie ?
@Override
public void stopCalculation() {
    flag = true;
}

我的帖子 class 看起来像这样:

public class LeibnizThread extends Thread {

private int threadRemainder;
private int numThreads;
private BigDecimal result = new BigDecimal(0.0);
private volatile boolean flag = false;


public LeibnizThread(int threadCount, int threadRemainder) {
    this.numThreads = threadCount;
    this.threadRemainder = threadRemainder;
}


public void run() {

    BigDecimal counter = new BigDecimal("1");

    while( !flag )  {
        if(counter.intValue() % numThreads == threadRemainder)
            if(counter.remainder(new BigDecimal("2")).equals(BigDecimal.ONE)) {
                result = result.add(BigDecimal.ONE.divide(((new BigDecimal("2").multiply(counter).subtract(BigDecimal.ONE))), 100, RoundingMode.HALF_UP));
            }else {
                result = result.subtract(BigDecimal.ONE.divide(((new BigDecimal("2").multiply(counter).subtract(BigDecimal.ONE))), 100, RoundingMode.HALF_UP));
            }
        counter = counter.add(new BigDecimal("1"));

    }
}
public BigDecimal getResult() {
    return this.result;
}

public void setFlagTrue() {
    flag = true;
}

} 我试图实现一个 "flag" 以使其停止,但我不知道如何影响在方法 "startCalculation(numThreads)" 中从方法 "stopCalculation()" 初始化的线程。

如果有人有想法请告诉我。祝你有美好的一天并保持健康:)

前言;我还没有构建 运行 你的代码,我也没有真正查阅莱布尼茨公式,所以我将保留这个对你的线程问题的答案。

看来您在这里遇到了两个问题:

  1. 调用 w.join() 将导致您的执行等待线程完成。不幸的是,线程永远不会完成,因为您永远不会退出 startCalculation()。这称为死锁,它是在一个线程永远等待另一个线程完成时引起的。
  2. 即使执行到了那个点,您也不知道如何让线程停止。

对于第一个问题,我的建议是使用 Java 的另一个有用线程 classes。在这种情况下,您应该更改 LeibnizThread 以实现 Runnable 而不是扩展 Thread。这仍然会导致创建一个新线程,但您通常不需要担心具体细节。

对于第二个问题,您可以将线程数组移出方法,使其范围限定在 class 级别。然后,在 stopCalculation() 中,您可以遍历线程并告诉它们停止。

我写了一个基本框架,说明在这种情况下如何使用 Runnable。请注意,这只是一种方法,Java 的并发库中有大量有用的 classes。所以环顾四周,看看所有可用的工具!

package com.sandbox;

import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Leibniz implements CalculatePi {
    private Worker[] workers;

    @Override
    public boolean startCalculation(int numThreads) {
        // The executor service handles your thread execution for you
        ExecutorService executorService = Executors.newFixedThreadPool(numThreads);

        // Start you threads and save a reference to them so you can call them later
        workers = new Worker[numThreads];
        for (int i = 0; i < numThreads; i++) {
            Worker worker = new Worker();
            workers[i] = worker;
            executorService.submit(worker); // This starts the thread.  It calls worker.run().
        }

        return true;
    }

    @Override
    public void stopCalculation() {
        for (Worker worker : workers) {
            worker.stopExecution();
        }
    }

    @Override
    public BigDecimal getValue() {
        BigDecimal result = BigDecimal.ZERO;
        for (Worker worker : workers) {
            // Do whatever thread consolidation work you need to do here to get a single result
            result = result.max(worker.getCurrentResult());
        }
        return result;
    }

    private class Worker implements Runnable {
        private volatile boolean stopExecution = false; // "volatile" helps make sure the thread actually stops when you want it to by avoiding CPU caches
        private BigDecimal currentResult;

        Worker() {
            // Pass in whatever you need to do the work
        }

        @Override
        public void run() {
            while (!stopExecution) {
                // Do all of your multi-threaded computation here, setting the currentResult as you go
                currentResult = new BigDecimal(System.currentTimeMillis()); // Example.
            }
        }

        void stopExecution() {
            this.stopExecution = true;
        }

        BigDecimal getCurrentResult() {
            return currentResult;
        }
    }
}

这里有一些练习它的代码。有点像你教授的代码。

    public static void main(String[] args) throws InterruptedException {
        CalculatePi pi = new Leibniz();
        pi.startCalculation(4);

        for (int i = 0; i < 5; i++) {
            sleep(1000);
            System.out.println("Current Result: " + pi.getValue());
        }

        pi.stopCalculation();

        BigDecimal finalResult = pi.getValue();
        sleep(1000);
        BigDecimal verifyFinalResult = pi.getValue();
        System.out.println("Workers actually stopped: " + finalResult.equals(verifyFinalResult));
    }

结果:

Current Result: 1586477696333
Current Result: 1586477697785
Current Result: 1586477698815
Current Result: 1586477699783
Current Result: 1586477700859
Workers actually stopped: true

我遗漏了很多内容,因为我不想为您做功课,但这应该可以帮助您入门。享受! :)