Menu

[Solved]Java Multithreading Several Threads Share Single Object Contribute Individual Result Share Q37246861

Java Multithreading

Several threads will share a single object and contribute theirindividual result to the shared object. The shared objectaccumulates the partial results.
Create a class named SharedResults as follows. The class keepstrack of the shared result. a. The instance (or member) privatevariable – result (int). b. A void addToResult method which takesthe given integer argument and adds it to the shared result. Thismethod then prints to the console the name of the current thread,the value it added, and the cumulative result. Handle thesynchronization issue with this method. c. The getResult methodwith no arguments which returns the shared result. Handle thesynchronization issue with this method.

2. Create a class named LongTask which extends the Thread class. a.The instance (or member) private variables – sharedData (of typeSharedResults), start (integer) and end (integer). b. A singleconstructor which takes the above three arguments and stores themin the instance values. Also, create a name for this thread asThread_<start>_<end> c. In the run method, add theinteger numbers from start to end (both inclusive) using a forloop. Also, sleep for a random time
(up to 10 milliseconds) in each iteration of the loop. After theloop, invoke the addToResult method of the shared object andprovide this accumulated sum.

3. Use your Java_Multithreading class to test the followingfunctionality in its main method. a. Create the SharedResultsobject and assign it to a variable. b. Create five LongTask objectsby passing the above shared object and the start and end values foreach as (1, 100), (101, 200), (201, 300), (301, 400), and (401,500) respectively. c. Start each thread as it is created. d. Waitfor all the threads to complete using the join method. e. Print theresult from the shared object.
       Different runs of the programwill produce the output in different sequences, but the resultwould be the same.Two runs of the program are shown below.Thread 1 100 is adding 5050, Cumulative Result is 5050 Thread 301400 is adding 35050, Cumulative Result is 40100 Thread 101_2

Thread 1 100 is adding 5050, Cumulative Result is 5050 Thread 301400 is adding 35050, Cumulative Result is 40100 Thread 101_200 is adding 15050, Cumulative Result is 55150 Thread 201_300 is adding 25050, Cumulative Result is 80200 Thread 401_500 is adding 45050, Cumulative Result is 125250 Result – 125250 Thread 401_500 is adding 45050, Cumulative Result is 45050 Thread_201_300 is adding 25050, Cumulative Result is 70100 Thread_301_400 is adding 35050, Cumulative Result is 105150 Thread 1_100 is adding 5050, Cumulative Result is 110200 Thread 101_200 is adding 15050, Cumulative Result is 125250 Result-125250 Show transcribed image text Thread 1 100 is adding 5050, Cumulative Result is 5050 Thread 301400 is adding 35050, Cumulative Result is 40100 Thread 101_200 is adding 15050, Cumulative Result is 55150 Thread 201_300 is adding 25050, Cumulative Result is 80200 Thread 401_500 is adding 45050, Cumulative Result is 125250 Result – 125250 Thread 401_500 is adding 45050, Cumulative Result is 45050 Thread_201_300 is adding 25050, Cumulative Result is 70100 Thread_301_400 is adding 35050, Cumulative Result is 105150 Thread 1_100 is adding 5050, Cumulative Result is 110200 Thread 101_200 is adding 15050, Cumulative Result is 125250 Result-125250

Expert Answer


Answer to Java Multithreading Several threads will share a single object and contribute their individual result to the shared obje… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *