Question: In Lab #3 you designed a multithreaded application that that had the child thread generate the Collatz sequence, and store it to a linked list.

In Lab #3 you designed a multithreaded application that that had the child thread generate the Collatz sequence, and store it to a linked list. Once the child thread terminated, the main thread output the contents of the sequence. The sharing between the child & parent threads occurred as all threads in the same application share global data. This lab will involve completing this task above using Java threads. You will design two solutions: Java opts for an object-oriented solution for sharing between threads. write a java program based upon using Callable/Future, where the child thread will generate the sequence, but place it in a List. The main thread will invoke the get() method of the Future, and then output the list. This will be loosely based upon the following program:

import java.util.concurrent.*; class Summation implements Callable { private int upper; public Summation(int upper) { this.upper = upper; } public Integer call() { int sum = 0; for (int i = 1; i <= upper; i++) sum += i; return Integer.valueOf(sum); } } public class Attempt1 { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: java Attempt1 "); System.exit(0); } else { int upper = Integer.parseInt(args[0]); ExecutorService pool = Executors.newSingleThreadExecutor(); Future result = pool.submit(new Summation(upper)); try { System.out.println("sum = " + result.get()); } catch (InterruptedException | ExecutionException ie) { } // Notice the difference if shutdown() is invoked. pool.shutdown(); } } }

This will require using a Java List object, and the following illustrates some of the uses of generics that will be necessary: You will design a class that implements the Callable interface, and returns a List of Integer values, ie

classs Collatz implements Callable> { }

The call() method in the Collatz class will return a List of Integer values, ie

public List call() { }

The call() method will run as a separate thread, and will populate an ArrayList of containing the integer values of the sequence. The main thread will invoke the get() method to retrieve the the List generated by the child thread, and output the contents of the List. (An iterator is a suggested approach.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!