Expert answer:java thread programming

Solved by verified expert:Programming Project 1In this programming project, you’ll using the Java Runtime to start another Java program. The other Java program is a multithreaded program in which the number of threads you specify work together to perform a task.The Starter.java program:The first program you will write will be called Starter.java. This program should not be in a package. This program will do four tasks:Read from the user that name of a file containing a list of numbers and the number of threads for the Worker program to create. (The number of threads should be an integer between two and 20.)Get the local Runtime class object using the getRuntime() method.Use the local runtime object to start your multithreaded program using one of the exec() methods (the simplest to use is exec(String [] cmdarray), where cmdarray[0] is the command to run and the remaining cmdarray elements are the arguments to the command. For this program, cmdarray[0] will hold “java”, the java runtime environment command, cmdarray[1] will hold “Worker”, the name of the multithreaded program, cmdarray[2] will hold the number of threads to use and cmdarray[3] will hold the name of the file from which the numbers are read..Tie the output of Worker program to the Starter program using the Process class method getInputStream() on the Worker process you created in step 2 and read the results that the Work program writes and display them.The Worker.java program:The second program you will write will be called Worker.java, again with no package. The Worker program will perform the following tasks in the main method:Get the command line arguments for Worker: the number of threads to create and the filename to read, from the main method parameter and place them in variables.Open the file for reading and then, read and store the number in the file. You will probably find it easiest to use an ArrayList to store the numbers.Create the specified number of threads, using the Runnable class you write and create an double array with an element for each thread to store its results.Start the thread you created.Using the Thread join() method, wait for each of the thread to complete.Sum the values in the results array and display the sum of the numbers that were read.You also have to write a class that implements the Runnable interface that the main method uses to create the threads. You should make this class an inner class of the Worker class. This will allow the Runnable class to have access to the static variables of the Worker class, thereby allows the running threads access to threse variables as shared data. The three pieces of shared data needed are the total number of threads, the list of numbers and the result array. The Runnable class should have a private instance variable which will hold the index of the thread object, which will be a number between 0 and the number of threads minus one. This index is assigned by the main method when it create the Runnable class for each thread. The index will be used by the thread object to write its final result to the proper element of the shared result array array and to partition the numbers in the numbers list.The Runnable class should be written so that it does the following:Based on its index, the thread decides which of the numbers it will process. If the number of threads is T and the size of the numbers list is N , then thread iwill process elements i through i+(NT−1) . For instance, if is 4 and is 39, then thread 0 will process elements 0 through 8, thread 1 will process elements 9 through 17, thread 2 will process elements 18 through 26, and thread 3 will process elements 28 through 39. Note that the last thread may process more elements than the others.Compute the sum of the numbers it is to process.Store the sum into the result array element using the thread index.
pp1__1_.pdf

Unformatted Attachment Preview

Prof. Pitts
CS554AH1: Operating Systems
Fall 2017
Programming Project 1
In this programming project, you’ll using the Java Runtime to start another Java program. The other Java program is a
multithreaded program in which the number of threads you specify work together to perform a task.
The Starter.java program:
The first program you will write will be called Starter.java. This program should not be in a package. This program will do
four tasks:
1.
Read from the user that name of a file containing a list of numbers and the number of threads for the Worker
program to create. (The number of threads should be an integer between two and 20.)
2.
Get the local Runtime class object using the getRuntime() method.
3.
Use the local runtime object to start your multithreaded program using one of the exec() methods (the simplest to
use is exec(String [] cmdarray), where cmdarray[0] is the command to run and the remaining cmdarray elements
are the arguments to the command. For this program, cmdarray[0] will hold “java”, the java runtime environment
command, cmdarray[1] will hold “Worker”, the name of the multithreaded program, cmdarray[2] will hold the
number of threads to use and cmdarray[3] will hold the name of the file from which the numbers are read..
4.
Tie the output of Worker program to the Starter program using the Process class method getInputStream() on the
Worker process you created in step 2 and read the results that the Work program writes and display them.
The Worker.java program:
The second program you will write will be called Worker.java, again with no package. The Worker program will perform
the following tasks in the main method:
1.
Get the command line arguments for Worker: the number of threads to create and the filename to read, from the
main method parameter and place them in variables.
2.
Open the file for reading and then, read and store the number in the file. You will probably find it easiest to use an
ArrayList to store the numbers.
3.
Create the specified number of threads, using the Runnable class you write and create an double array with an
element for each thread to store its results.
4.
Start the thread you created.
5.
Using the Thread join() method, wait for each of the thread to complete.
6.
Sum the values in the results array and display the sum of the numbers that were read.
You also have to write a class that implements the Runnable interface that the main method uses to create the threads. You
should make this class an inner class of the Worker class. This will allow the Runnable class to have access to the static
variables of the Worker class, thereby allows the running threads access to threse variables as shared data. The three pieces
of shared data needed are the total number of threads, the list of numbers and the result array. The Runnable class should
have a private instance variable which will hold the index of the thread object, which will be a number between 0 and the
number of threads minus one. This index is assigned by the main method when it create the Runnable class for each thread.
The index will be used by the thread object to write its final result to the proper element of the shared result array array and
to partition the numbers in the numbers list.
The Runnable class should be written so that it does the following:
1.
Based on its index, the thread decides which of the numbers it will process. If the number of threads is
size of the numbers list is
is 4 and
T and the
N
N , then thread i will process elements i through i+(T −1) . For instance, if T
N is 39, then thread 0 will process elements 0 through 8, thread 1 will process elements 9 through 17,
1/2
Prof. Pitts
CS554AH1: Operating Systems
Fall 2017
thread 2 will process elements 18 through 26, and thread 3 will process elements 28 through 39. Note that the last
thread may process more elements than the others.
2.
Compute the sum of the numbers it is to process.
3.
Store the sum into the result array element using the thread index.
Resources
The Oracle Java documentation website has reference pages for the Runtime, Process, and Thread classes. Note that
Runtime, Process, and Thread are in the java.lang package which automatically imported by all Java programs; you do not
need an import statement for these classes.
To read a text file in Java, it is probably most convenient to use the Java Scanner class hasNextDouble() and nextDouble()
methods. Wrap the Scanner object you create around a FileReader object. The Scanner class is in java.util and the
FileReader class is in java.io.
The primary links for the five Java classes are:

Runtime: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

Process: https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

Thread: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html

Scanner: https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

FileReader: https://docs.oracle.com/javase/8/docs/api/java/io/FileReader.html
You can any number of tutorials on these topics. I expect you to do your own research on how these classes work before
you ask me questions.
Deliverables:
Submit a ZIP file containing your two Java program files Starter.java and Worker.java to Canvas by the due date.
2/2

Purchase answer to see full
attachment

How it works

  1. Paste your instructions in the instructions box. You can also attach an instructions file
  2. Select the writer category, deadline, education level and review the instructions 
  3. Make a payment for the order to be assignment to a writer
  4.  Download the paper after the writer uploads it 

Will the writer plagiarize my essay?

You will get a plagiarism-free paper and you can get an originality report upon request.

Is this service safe?

All the personal information is confidential and we have 100% safe payment methods. We also guarantee good grades

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more

Order your essay today and save 20% with the discount code ESSAYHELP