0% found this document useful (0 votes)
4 views

# ex2.java

The document contains a Java program that calculates the sum of integers from 1 to a specified number using multiple threads. It defines a `SumThread` class to handle the summation in parallel, dividing the workload among a specified number of threads. The main method initializes the array and starts the threads, then aggregates their results to produce the final sum.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

# ex2.java

The document contains a Java program that calculates the sum of integers from 1 to a specified number using multiple threads. It defines a `SumThread` class to handle the summation in parallel, dividing the workload among a specified number of threads. The main method initializes the array and starts the threads, then aggregates their results to produce the final sum.

Uploaded by

hoangtu112201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# ex2.

java
class SumThread extends Thread {
int lo; // fields for communicating inputs
int hi;
int[] arr;
int ans = 0; // for communicating result
SumThread(int[] a, int l, int h) {
lo=l; hi=h; arr=a;
}
public void run() { // overriding, must have this type
for(int i=lo; i<hi; i++)
ans += arr[i];
}
}

class ex2 {

private static int NUM_END=10000;


private static int NUM_THREAD=4;

public static void main(String[] args) {


if (args.length==2) {
NUM_THREAD = Integer.parseInt(args[0]);
NUM_END = Integer.parseInt(args[1]);
}
System.out.println("number of threads:"+NUM_THREAD);
System.out.println("sum from 1 to "+NUM_END+"=");

int[] int_arr = new int [NUM_END];


int i,s;

for (i=0;i<NUM_END;i++) int_arr[i]=i+1;


s=sum(int_arr);
System.out.println(s);
}

static int sum(int[] arr) {


int len = arr.length;
int ans = 0;
SumThread[] ts = new SumThread[NUM_THREAD];
for(int i=0; i < NUM_THREAD; i++) {
ts[i] = new SumThread(arr,(i*len)/NUM_THREAD,((i+1)*len)/NUM_THREAD);
ts[i].start();
}
try {
for(int i=0; i < NUM_THREAD; i++) {
ts[i].join();
ans += ts[i].ans;
}
} catch (InterruptedException IntExp) {
}

return ans;
}
}

Beta
0 / 0
used queries
1

You might also like