Sunday, May 23, 2021

Multi thread and Concurrency - join

<p>In this coding exercise we will you all the knowledge from the previous lectures. <br>Before taking the exercise make sure you review the following topics in particular:<br>1. Thread Creation - how to create and start a thread using the <code>Thread</code> class and the <code>start()</code> method.</p>


<p>2. Thread Join - how to wait for another thread using the <code>Thread.join()</code> method.</p>


<p>In this exercise we will efficiently calculate the following <code>result = base1 ^ power1 +&nbsp;base2 ^ power2</code></p>


<p>Where <code>a^b</code> means: <em>a</em> raised to the power of <em>b. </em></p>

<p>For example <code>10^2 = 100</code></p>

<p>We know that raising a number to a power is a complex computation, so we we like to execute:</p>

<p><code>result1 = x1 ^ y1</code></p>

<p><code>result2 = x2 ^ y2</code></p>

<p>In parallel.</p>

<p>and combine the result in the end : <code>result = result1 +&nbsp;result2</code></p>

<p>This way we can speed up the entire calculation.</p>



-------------------------------------------------


package thread.example;

import java.math.BigInteger;

public class ComplexCalculation {
public static void main(String[] args) {
ComplexCalculation c = new ComplexCalculation();
BigInteger result = c.calculateResult(new BigInteger("2"),new BigInteger("2"),new BigInteger("2"),new BigInteger("3"));
System.out.println(result);
}
public BigInteger calculateResult(BigInteger base1, BigInteger power1, BigInteger base2, BigInteger power2) {
BigInteger result;
/*
Calculate result = ( base1 ^ power1 ) + (base2 ^ power2).
Where each calculation in (..) is calculated on a different thread
*/
PowerCalculatingThread one = new PowerCalculatingThread(base1, power1);
PowerCalculatingThread two = new PowerCalculatingThread(base2, power2);
one.start();
two.start();

try {
one.join();
two.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
result = one.getResult().add(two.getResult());
return result;
}

private static class PowerCalculatingThread extends Thread {
private BigInteger result = BigInteger.ONE;
private BigInteger base;
private BigInteger power;

public PowerCalculatingThread(BigInteger base, BigInteger power) {
this.base = base;
this.power = power;
}

@Override
public void run() {
/*
Implement the calculation of result = base ^ power
*/
this.result = this.base.pow(this.power.intValue());

// result = BigInteger.ONE;
//
// for(BigInteger i = BigInteger.ZERO;
// i.compareTo(power) !=0;
// i = i.add(BigInteger.ONE)) {
// result = result.multiply(base);
// }
}

public BigInteger getResult() {
return result;
}
}

} 


2. Thread Join - how to wait for another thread using the Thread.join() method.

In this exercise we will efficiently calculate the following result = base1 ^ power1 + base2 ^ power2

Where a^b means: a raised to the power of b.

For example 10^2 = 100

We know that raising a number to a power is a complex computation, so we we like to execute:

result1 = x1 ^ y1

result2 = x2 ^ y2


In parallel.

and combine the result in the end : result = result1 + result2

This way we can speed up the entire calculation.


Note :

base1 >= 0, base2 >= 0, power1 >= 0, power2 >= 0

No comments:

Post a Comment