简体   繁体   中英

Thread prints results after Main method finishes

The final result of my calculation is 0 but when I print the result in my run() method, the correct result appears after my main method finished. Does anyone know why this keeps happening?

    @Override
public void run() {

    BigDecimal counter = BigDecimal.ZERO;
    BigDecimal numOfAllThreadsBigDecimal = new BigDecimal(getNumOfAllThreads());
    BigDecimal numOfThreadBigDecimal = new BigDecimal(getNumOfThread());
    BigDecimal numerator = BigDecimal.ZERO;
    BigDecimal divisor = BigDecimal.ZERO;
    BigDecimal endResult = BigDecimal.ZERO;

    while (!isFlag()) {
        // counter % numOfAllThreads == numOfThread
        if (counter.remainder(numOfAllThreadsBigDecimal).equals(numOfThreadBigDecimal)) {
            // (4 * counter)! * (1103 + 26390 * counter)
            numerator = BigDecimalFactorial.factorial((counter.multiply(new BigDecimal(4)))).multiply(new BigDecimal(1103).add((new BigDecimal(26390).multiply(counter))));
            // (counter!)^4 * 396^4*counter
            divisor =  ((BigDecimalFactorial.factorial(counter)).pow(4)).multiply( (new BigDecimal(396).pow(4 * counter.intValue())) ) ;
            // numerator / divisor
            endResult = endResult.add(((numerator.divide(divisor, 100, RoundingMode.HALF_UP))));

            internalSteps++;

        }
        counter = counter.add(BigDecimal.ONE);
    }
    // multiply endResult by sqrt(8) / 9801
    endResult = endResult.multiply(((BigDecimalSqrt.bigSqrt(new BigDecimal(8))).divide(new BigDecimal(9801),100,RoundingMode.HALF_UP)));
    // divide 1 / pi
    endResult = BigDecimal.ONE.divide(endResult, 100, RoundingMode.HALF_UP);

    System.out.println(endResult);

    setThreadResult(endResult);
}


    public BigDecimal getValue() {

    BigDecimal result = BigDecimal.ZERO;

    for(Ramanujan worker : workers)
        result = result.add(worker.getThreadResult());
    this.value = result;

    return this.value;
}


    public static void main(String[] args) {

    CalculatePi pi = new Ramanujan();

    pi.startCalculation();
    long timeStart = System.currentTimeMillis();

    int i = 0;
    while(i < MAX_PRECISION) {
        someDelay();
        i++;
    }
    long timeStop = System.currentTimeMillis();
    pi.stopCalculation();
    System.out.println((timeStop - timeStart) + " ms");
    System.out.println("Result: " + pi.getValue());
    System.out.println("IternalSteps: " + pi.getInternalSteps());

}

The outcome on the Console is:

10004 ms (that's correct) 
Result: 0
InternalSteps: 2088
3.141592653589793...

The number 3.141592653589793 should come up at the result row.

When you start your thread, it runs in parallel with main thread. If you want to stop main thread from execution till the other thread finishes its work, there are several approaches, but the simplest one is using join method. for further information read java documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM