Question: design a java program that creates a class that runs as a separate thread. The main thread (i.e. the one that runs in main() )

design a java program that creates a class that runs as a separate thread. The main thread (i.e. the one that runs in main() ) will read in an input argument and will pass it to the child thread that it creates. The child thread will generate and output the Collatz sequence from the starting value it is passed. This will not involve using thread pools, but rather directly creating a thread and invoking its start() method. This will be roughly based upon the following program:

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 using Java threads. You will design two solutions: Version 1 This will involve designing a program that creates a class that runs as a separate thread. The main thread (i.e. the one that runs in main() ) will read in an input argument and will pass it to the child thread that it creates. The child thread will generate and output the Collatz sequence from the starting value it is passed. This will not involve using thread pools, but rather directly creating a thread and invoking its start() method. This will be roughly based upon the following program:

class Sum { private int sum; public int get() { return sum; } public void set(int sum) { this.sum = sum; } } /** * This runs as a separate Java thread. * * This performs a summation from 1 .. upper */ class Summation implements Runnable { private int upper; private Sum sumValue; public Summation(int upper, Sum sumValue) { this.upper = upper; this.sumValue = sumValue; } public void run() { int sum = 0; for (int i = 1; i <= upper; i++) sum += i; sumValue.set(sum); } } public class Driver { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage java Driver "); System.exit(0); } if (Integer.parseInt(args[0]) < 0) { System.err.println(args[0] + " must be >= 0"); System.exit(0); } // Create the shared object Sum sumObject = new Sum(); int upper = Integer.parseInt(args[0]); Thread worker = new Thread(new Summation(upper, sumObject)); worker.start(); try { worker.join(); } catch (InterruptedException ie) { System.err.println(ie); } System.out.println("The sum of " + upper + " is " + sumObject.get()); } }

except that you do not have to use the separate Sum class as the child thread will both generate and output the Collatz sequence.

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!