Question

In: Computer Science

This project is much easier if you create an algorithm. If you understand the content of...

This project is much easier if you create an algorithm.

If you understand the content of the Concurrency Basics Tutorial, this Project will be easy. If not, 40 hours of working on it won't help. I suggest reading the instructions slowly and carefully before reading the tutorial so that you will notice what is needed as you read. Then read them again, slowly and carefully.

Using the concepts from the Concurrency Basics Tutorial I provided in Modules, write a program that consists of two threads. The first is the main thread that every Java application has. The main thread should create a new thread from the Runnable object, MessageLoop, and wait for it to finish. If the MessageLoop thread takes too long to finish, the main thread should interrupt it. Use a variable named maxWaitTime to store the maximum number of seconds to wait. The main thread should output a message stating that it is still waiting every half second.

The MessageLoop thread should print out a series of 4 messages. These messages should be numbered, as in the example below. It should wait 850 milliseconds between printing messages to create a delay. If it is interrupted before it has printed all its messages, the MessageLoop thread should print "Message loop interrupted" and exit. Or you can let main print "Message loop interrupted".

Your program must demonstrate that it can both output messages and interrupt the message output. To do this, place the body of main into a for loop using maxWaitTime as the index. As in the following example, it should finally output all 4 messages in the last iteration.

So in main your code will be

for (int maxWaitTime = 1; maxWaitTime <= 4; maxWaitTime++) {

// All of main's processing goes here (Note that it does not say some, it says all).

}

Sample output :

maxWaitTime: 1 second(s)
main : Starting MessageLoop thread
main : Waiting for MessageLoop thread to finish
main : Continuing to wait...
main : Continuing to wait...
Thread-0 : 1. All that is gold does not glitter, Not all those who wander are lost
main : MessageLoop interrupted
maxWaitTime: 2 second(s)
main : Starting MessageLoop thread
main : Waiting for MessageLoop thread to finish
main : Continuing to wait...
main : Continuing to wait...
Thread-1 : 1. All that is gold does not glitter, Not all those who wander are lost
main : Continuing to wait...
main : Continuing to wait...
Thread-1 : 2. The old that is strong does not wither, Deep roots are not reached by the frost
main : MessageLoop interrupted
maxWaitTime: 3 second(s)
main : Starting MessageLoop thread
main : Waiting for MessageLoop thread to finish
main : Continuing to wait...
main : Continuing to wait...
Thread-2 : 1. All that is gold does not glitter, Not all those who wander are lost
main : Continuing to wait...
main : Continuing to wait...
Thread-2 : 2. The old that is strong does not wither, Deep roots are not reached by the frost
main : Continuing to wait...
main : Continuing to wait...
Thread-2 : 3. From the ashes a fire shall be woken, A light from the shadows shall spring
main : MessageLoop interrupted
maxWaitTime: 4 second(s)
main : Starting MessageLoop thread
main : Waiting for MessageLoop thread to finish
main : Continuing to wait...
main : Continuing to wait...
Thread-3 : 1. All that is gold does not glitter, Not all those who wander are lost
main : Continuing to wait...
main : Continuing to wait...
Thread-3 : 2. The old that is strong does not wither, Deep roots are not reached by the frost
main : Continuing to wait...
main : Continuing to wait...
Thread-3 : 3. From the ashes a fire shall be woken, A light from the shadows shall spring
main : Continuing to wait...
Thread-3 : 4. Renewed shall be blade that was broken
main : Done!

Your class must be in a package named mypackage and be named Concurrency, as explained in last week's videos. It should be contained in 1 and only 1 source file.

Include your name at the top of the source file.

Upload Concurrency.java

Solutions

Expert Solution

public class Concurrency implements Runnable{

static int count=-1;

public void run(){

String msg[]={"1.All that is gold does not glitter,Not all those who wander are lost","2.The old that is strong does not wither,Deep roots are not reached by the frost","3.From the ashes a fire shall be woken,A light from the shadows shall spring","4.Renewed shall be blade that was broken"};

try{

for(int j=0;j<4;j++)

{

System.out.println("Thread-"+count+":"+msg[j]);

Thread.sleep(850);

}

}

catch(InterruptedException e)

{

System.out.println("main:MessageLoop Interrupted");

}

}

public static void main(String[] args) {

for(int maxWaitTime=1;maxWaitTime<=4;maxWaitTime++){

count++;

System.out.println("maxWaitTime:"+maxWaitTime+"second(s)");

Concurrency MessageLoop=new Concurrency();

Thread thread=new Thread(MessageLoop);

System.out.println("main:Starting MessageLoop thread");

System.out.println("main:Waiting for MessageLoop thread to finish");

thread.start();

for(int i=1;i<=maxWaitTime*2;i++)

{

try{

Thread.sleep(500);

}

catch(InterruptedException e){

System.out.println(e);

}

System.out.println("main:Continuing to wait...");

}

thread.interrupt();

}

System.out.println("main:Done!");

}

}


Related Solutions

Since the Direct Method is much easier to understand, why do you think that more companies...
Since the Direct Method is much easier to understand, why do you think that more companies use the Indirect Method to prepare the Operating Activities section of the Cash Flow Statement?
Lesson Assignment There's not much content to this lesson other than to create the table of...
Lesson Assignment There's not much content to this lesson other than to create the table of words and counts (which will be a list of tuples). The words are already parsed out for you (same as the previous lesson). Build the following three functions: def clean(words): normalizes the words so that letter case is ignored returns an array of 'cleaned' words def build_table(words): builds a dictionary of counts returns a Python dictionary or collections.Counter type def top_n(table, n): returns the...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in...
Create a new Visual Studio console project named assignment042, and translate the algorithm you developed in Assignment 04.1 to C++ code inside the main() function. Your program should prompt for a single 9-digit routing number without spaces between digits as follows: Enter a 9-digit routing number without any spaces: The program should output one of: Routing number is valid Routing number is invalid A C++ loop and integer array could be used to extract the routing number's 9 digits. However...
Please show your work so it is easier to understand, thank you! The payroll records of...
Please show your work so it is easier to understand, thank you! The payroll records of Airtech Solutions, Ltd. are provided for the period ended December 7: (See Payroll Liabilites on pages 404-406) Required: Compute the necessary information for the payroll journal entries. Complete the entry to accrue the payroll. Complete the entry to record the payroll tax expense. Employee Earnings to End of Previous Week Gross Pay Federal Income Taxes Medical Insurance Deduction Union Dues United Way R. Arthur...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that...
Question 1: Using Python 3 Create an algorithm The goal is to create an algorithm that can sort a singly-linked-list with Merge-sort. The program should read integers from file (hw-extra.txt) and create an unsorted singly-linked list. Then, the list should be sorted using merge sort algorithm. The merge-sort function should take the head of a linked list, and the size of the linked list as parameters. hw-extra.txt provided: 37 32 96 2 25 71 432 132 76 243 6 32...
Project 4 – The Nearest Neighbors Classification Algorithm This project will require you to implement a...
Project 4 – The Nearest Neighbors Classification Algorithm This project will require you to implement a version of the Nearest Neighbors classification algorithm. This version, the Three Nearest Neighbors (or 3NN for short), is one of the more intuitive classification algorithms, and one of the easier ones to code. This Nearest Neighbors family of algorithms is useful because it does not require any special “training” to work. You simply need previous data to compare to. What is classification? With classification,...
As a business owner it is important to understand not only how much revenue you are...
As a business owner it is important to understand not only how much revenue you are making but also the profit that is being realized.  Part of this process is to understand contribution margin, break-even point and operating income.   This assignment has 4 parts: What is taken into consideration to figure out the contribution margin? How is the break-even point determined? What is needed to determine the operating income? Would the contribution margin, break-even point and operating income change if...
1) Understand the problem 2) Develop and Describe an Algorithm 3) Test Algorithm with Simple Inputs...
1) Understand the problem 2) Develop and Describe an Algorithm 3) Test Algorithm with Simple Inputs 4) Translate the Algorithm into Java 5) Compile and Test Your Program 1) Understand the problem A client (a person who wants a program developed) who owns a painting company has requested that you create a prototype program that calculates the cans of paint required to paint a wall based on surface area in square feet. Normally a staff member at the store interacts...
You will utilize a large dataset to create a predictive analytics algorithm in Python. For this...
You will utilize a large dataset to create a predictive analytics algorithm in Python. For this assignment, complete the following: Utilize one of the following Web sites to identify a dataset to use, preferably over 500K from Google databases, kaggle, or the .gov data website Utilize a machine learning algorithm to create a prediction. K-nearest neighbors is recommended as an introductory algorithm. Your algorithm should read in the dataset, segmenting the data with 70% used for training and 30% used...
Homework Arrays and Tables In this assignment you are to create an algorithm, flowchart, and pseudocode...
Homework Arrays and Tables In this assignment you are to create an algorithm, flowchart, and pseudocode for a solution of the following problem. This solution will include the use of arrays needed to complete all parts of the logic. You have requested to develop a program that will record and process the rainfall totals of a 12 month period. You would use an array to store each months total. Once all 12 months amounts are entered then your solution need...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT