Reputation: 1779
I need to explain the Join function by calculating in parallel this equation D= (a-b)+(c-d). Suppose that I have an equation D= (a-b)+(c-d). How can I do this equation in parallel using three thread one to calculate (a-b), one to calculate (c-d) and the main thread to show the result. I need to show that the main does not do show the result before that two thread dead.
Upvotes: 0
Views: 2738
Reputation: 126
Actually you could do without join using thread pools (ExecutorService
):
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
public class Main {
static int calc(int a, int b, int c, int d) throws ExecutionException, InterruptedException {
var executor = ForkJoinPool.commonPool();
var f1 = executor.submit(() -> a - b);
var f2 = executor.submit(() -> c - d);
return f1.get() + f2.get();
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println(calc(1, 2, 3, 4));
}
}
... or with a different kind of join
from the CompletableFuture
framework (would also run on the Common Pool):
import java.util.concurrent.CompletableFuture;
public class Main {
static int calc(int a, int b, int c, int d) {
var s1 = CompletableFuture.supplyAsync(() -> a - b).join();
var s2 = CompletableFuture.supplyAsync(() -> a - b).join();
return s1 + s2;
}
public static void main(String[] args) {
System.out.println(calc(1, 2, 3, 4));
}
}
Upvotes: 0
Reputation: 9453
As the Javadoc says, join()
waits until a given thread dies—hence, it's a statement which blocks until a thread has finished computation. Using your equation:
// Choose a, b, c, and d.
int a = 0;
int b = 1;
int c = 2;
int d = 3;
// Set up an array for the intermediate results.
int[] results = new int[2];
// Create two threads writing the intermediate results.
Thread t0 = new Thread(() -> results[0] = a - b);
Thread t1 = new Thread(() -> results[1] = c - d);
// Start both threads.
t0.start();
t1.start();
// Let the main thread wait until both threads are dead.
try {
t0.join();
t1.join();
} catch (InterruptedException e) { /* NOP */ }
// Sum up the intermediate results and print it.
System.out.println(results[0] + results[1]);
Using a simple array to retrieve results from a thread is a bit fishy (check out this question). However, it's sufficient for this example.
Upvotes: 1
Reputation: 2234
I am creating Two threads they are paralyzed :
They are t1 and t2;
Here the main() compute the total sum :
this code may help you:
class SumThread extends Thread implements Runnable {
public SumThread(int a, int b) {
this.a = a;
this.b = b;
sum = 0;
}
public void run( ) {
sum=(a-b);
}
public int getSum( ) {
return sum;
}
private int a, b, sum;
}
public class Sum2 {
public static void main(String args[]) {
SumThread t1 = new SumThread(1, 2);
SumThread t2 = new SumThread(3, 4);
t1.start( );
t2.start( );
try {
t1.join( );
t2.join( );
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
System.out.printf("The sum %d \n", t1.getSum( )+t2.getSum());
}
}
Upvotes: 1