簡體   English   中英

多線程和停止線程

[英]Multi-threading and stopping a thread

我真的需要一些關於我的項目的幫助。 任務:測試的目的是使用由多線程加速的各種計算過程來創建 π (Pi) 計算。 使用 BigDecimal class 以獲得更好的精度。 使用您自己的異常類並將所有類打包在一個簡潔的 package 概念中。

我試圖實現萊布尼茨方法,我的主要問題是我不知道如何在線程運行時從我的主要方法中停止線程。 我的老師向我們展示了他的 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;
}

我試圖實現一個“標志”以使其停止,但我不知道如何從方法“stopCalculation()”對在方法“startCalculation(numThreads)”中初始化的線程產生影響。

如果有人有想法,請告訴我。 祝你有美好的一天,保持健康:)

序言; 我還沒有構建和運行你的代碼,也沒有真正查找過萊布尼茨公式,所以我只保留這個答案來回答你的線程問題。

看起來您在這里面臨兩個問題:

  1. 調用w.join()將導致您的執行等待線程完成。 不幸的是,線程永遠不會結束,因為您永遠不會退出startCalculation() 這稱為死鎖,它是由一個線程永遠等待另一個線程完成時引起的。
  2. 即使你的執行到了那個地步,你也不知道如何告訴線程停止。

對於第一個問題,我的建議是使用 Java 的其他有用的線程類之一。 在這種情況下,您應該更改 LeibnizThread 以實現Runnable而不是擴展Thread 這仍然會導致創建一個新線程,但您大多不需要擔心細節。

對於第二個問題,您可以將線程數組移出方法,使其范圍在 class 級別。 然后,在stopCalculation()中,您可以遍歷線程並告訴它們停止。

我為在這種情況下如何使用 Runnables 編寫了一個基本框架。 請注意,這只是一種方法,Java 的並發庫中有大量有用的類。 所以環顧四周,看看所有可用的工具!

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

我遺漏了很多,因為我不想為你做作業,但這應該可以幫助你開始。 享受: :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM