1. Create a class MySumCount which extends the Thread class. Add to class MySumCount two integer fields startIndex and stopIndex with setters and getters. Add to class MySumCount new field as array of integer type and setter for it. Add to class MySumCount new field resultSum of long type and getter for it.
2. Override the run() method of Thread. Add code to calculate sum of array elements from startIndex to stopIndex and save result to resultSum field.
3. Create a class Main with a main() method. Add to method main() code that declares local variable myArray as array of integer type (of 1000 size), and create two instances of MySumCount. Add code to fill all elements of myArray with random integer values generated from 0 to 1000 range. Add code to calculates sum of myArray elements in two separate thread and then printresult to console.
public class MySumCount extends Thread {
public int startIndex;
public int stopIndex;
public long resultSum;
public int[] array = {startIndex, stopIndex};
public long getResultSum() {
return resultSum;
}
public void setArray(int[] array) {
this.array = array;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getStopIndex() {
return stopIndex;
}
public void setStopIndex(int stopIndex) {
this.stopIndex = stopIndex;
}
@Override
public void run() {
for (int i = 0; i < array.length; i++)
resultSum += array[i];
System.out.println(resultSum);
}
}
public class Main {
public static void main(String[] args) {
int[] arr = new int[1000];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) Math.round((Math.random() * 1000));
System.out.print(arr[i] + " ");
}
class Playground {
static class Task extends Thread {
public long delay = 1000;
public int num = 0;
@Override
public void run() {
System.out.println("Started " + num);
try {
Thread.sleep(delay);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
System.out.println("Finished " + num);
}
}
public static void main(String[ ] args) throws InterruptedException {
final Task t1 = new Task();
t1.num = 1;
t1.delay = 2000;
final Task t2 = new Task();
t2.num = 2;
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Completed");
}
}