OOPS Lab-7 [ Multithreading]

Post date: Nov 6, 2017 10:53:20 AM

I. a) Develop a JAVA program to compute the sum of 'num' numbers using 'n' threads. The main thread reads the num and n from user and then creates n threads where each thread computes the sum of num/n nos and thn displays the final sum.

eg, if num = 1000, and n = 4, the t1 computes sum of 1..250, t2 from 251 to 500, t3 from 501 to 750 and t4 from 751 to 1000. The main thread display the final

sum. (after ensuring that each thread has finished its work).

II. Write a SearcherThr class which can do a multithreaded search (to search for an element in an array using treads), to be used in the main method as shown below.

public class Q3a {

public static void main(String[] args) {

int[] data = { 10, -2 , … };

SearcherThr s1 = new SearcherThr( data, element, startIdx1, endIdx1);

SearcherThr s2 = new SearcherThr( data, element, startIdx2, endIdx2);

s1.join();

s2.join();

SearcherThr.display(); //static method to display the found index.

}