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.
This is 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.
import java.io.*;
/*
* This runs as a separate Java thread.
*/
class Collatz implements Runnable
{
private int n;
//Constructor to receive value of n from main Thread
public Collatz(int n) {
this.n = n;
}
public void run() {
//loop to calculate Collatz Sequence
while (n != 1)
{
System.out.print(n + " ");
// If n is odd
if ((n & 1) == 1)
n = 3 * n + 1;
// If even
else
n = n / 2;
}
// Print 1 at the end
System.out.print(n);
}
}
class Main
{
public static void main(String[] args) {
//checking input to be single number
if (args.length != 1) {
System.err.println("Enter only single Number ");
System.exit(0);
}
//checking input number to be positive
if (Integer.parseInt(args[0]) < 0) {
System.err.println(args[0] + " must be >= 0");
System.exit(0);
}
//creating thread object and passing the value of
n
Thread t = new Thread(new
Collatz(Integer.parseInt(args[0])));
//Invoking the start() method
t.start();
}
}
In this program the main function first accepts a number from command line argument and checks it to be a single number and positive and then passes it to the child thread Collatz. Then in child thread the Collatz sequence is generated and printed. This program is programmed by taking help of the given program and also by doing some modification wherever required.
design a java program that creates a class that runs as a separate thread. The main...