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. 4

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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. 4 means 4. If you use 5 messages, you will receive a 0. 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" as in the sample output. The MessageLoop thread must try to output all 4 messages. Don't pass it a parameter that it uses to know when to stop printing messages. Only main should know that it might interrupt the MessageLoop thread.

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.

This program should not ask the user for any input or accept any command line arguments. This means don't use the args from public static void main(String args[]).

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

 

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. It is what it is
main : Done!

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY