In: Computer Science
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
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!");
}
}