In Java Language
from the Concurrency Basics Tutorial I provided inModules, write a program that consists of two threads. The first isthe main thread that every Java application has. The main threadshould create a new thread from the Runnable object, MessageLoop,and wait for it to finish. If the MessageLoop thread takes too longto finish, the main thread should interrupt it. Use a variablenamed maxWaitTime to store the maximum number of seconds to wait.The main thread should output a message stating that it is stillwaiting every half second.
The MessageLoop thread should print out a series of 4 messages.These messages should be numbered, as in the example below. Itshould wait 850 milliseconds between printing messages to create adelay. If it is interrupted before it has printed all its messages,the MessageLoop thread should print "Message loop interrupted" andexit. Or you can let main print "Message loop interrupted" as inthe sample output.
Your program must demonstrate that it can both output messagesand interrupt the message output. To do this, place the body ofmain into a for loop using maxWaitTime as the index. As in thefollowing example, it should finally output all 4 messages in thelast iteration.
This program should not ask the user for any input or accept anycommand line arguments. This means don't use the args from publicstatic 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 saysome, it says all).
}
Sample output :
maxWaitTime: 1 second(s)main : Starting MessageLoop threadmain : Waiting for MessageLoop thread to finishmain : Continuing to wait...main : Continuing to wait...Thread-0 : 1. All that is gold does not glitter, Not all those whowander are lostmain : MessageLoop interruptedmaxWaitTime: 2 second(s)main : Starting MessageLoop threadmain : Waiting for MessageLoop thread to finishmain : Continuing to wait...main : Continuing to wait...Thread-1 : 1. All that is gold does not glitter, Not all those whowander are lostmain : Continuing to wait...main : Continuing to wait...Thread-1 : 2. The old that is strong does not wither, Deep rootsare not reached by the frostmain : MessageLoop interruptedmaxWaitTime: 3 second(s)main : Starting MessageLoop threadmain : Waiting for MessageLoop thread to finishmain : Continuing to wait...main : Continuing to wait...Thread-2 : 1. All that is gold does not glitter, Not all those whowander are lostmain : Continuing to wait...main : Continuing to wait...Thread-2 : 2. The old that is strong does not wither, Deep rootsare not reached by the frostmain : Continuing to wait...main : Continuing to wait...Thread-2 : 3. From the ashes a fire shall be woken, A light fromthe shadows shall springmain : MessageLoop interruptedmaxWaitTime: 4 second(s)main : Starting MessageLoop threadmain : Waiting for MessageLoop thread to finishmain : Continuing to wait...main : Continuing to wait...Thread-3 : 1. All that is gold does not glitter, Not all those whowander are lostmain : Continuing to wait...main : Continuing to wait...Thread-3 : 2. The old that is strong does not wither, Deep rootsare not reached by the frostmain : Continuing to wait...main : Continuing to wait...Thread-3 : 3. From the ashes a fire shall be woken, A light fromthe shadows shall springmain : Continuing to wait...Thread-3 : 4. It is what it ismain : Done!
In Java Language from the Concurrency Basics Tutorial I provided in Modules, write a program that consists of two thread
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
In Java Language from the Concurrency Basics Tutorial I provided in Modules, write a program that consists of two thread
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!