Question: Java programming question about concurrency and 2 threads. Write a program that consists of two threads. The first is the main thread that every Java
Java programming question about concurrency and 2 threads.
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 second (or a different value so that it will output this message a few times, depending on your other wait times).
The MessageLoop thread should print out a series of 5 messages. It should wait a few seconds between printing messages to create a delay. Make the value sufficiently high to cause main to output Continuing to wait messages. If it is interrupted before it has printed all its messages, the MessageLoop thread should print "Message loop interrupted" and exit.
Your program must demonstrate that it can both output messages and interrupt the message output. To do this, place your code in main into a for loop using maxWaitTime as the index. Choose values that will demonstrate this. You can iterate this loop 2 times or 10 times, as many as needed to demonstrate the above requirement.
Sample output : maxWaitTime : 1 second(s)
main: Starting MessageLoop thread main: Waiting for MessageLoop thread to finish main: Continuing to wait... main: Interrupting Thread-0: Message loop interrupted main: Finished!
maxWaitTime : 2 second(s) main: Starting MessageLoop thread main: Waiting for MessageLoop thread to finish main: Continuing to wait... main: Continuing to wait... Thread-0: Message 1 main: Continuing to wait... main: Continuing to wait... Thread-0: Message 2 main: Continuing to wait... main: Continuing to wait... Thread-0: Message 3 main: Continuing to wait... main: Continuing to wait... Thread-0: Message 4 main: Continuing to wait... main: Continuing to wait... Thread-0: Message 5 main: Finished!
///////////////////////////////////////////////////////////////////////////////////////////////////////////////// Here is an incorrect answer, but it will help you get started with some of the code. It is just 2 separate classes that make the correct output, but are not built according to the assignment. Both answers need to be 1 class with 2 threads. Also, this code never uses maxWaitTime, which is has to use as the index. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
This will produce the first desired output:
package thread;
public class MessageLoop implements Runnable { public void run() { try { for(int i=1;i<=5;i++) { Thread.sleep(2100); System.out.println("Thread-0: Message "+(i)); } } catch (InterruptedException ex) { System.out.println("Thread-0: Message loop interrupted"); } } public static void main(String[] args) { System.out.println("main: Starting MessageLoop thread"); //create a thread Thread t1 = new Thread(new MessageLoop()); //start the thread t1.start(); System.out.println("main: Waiting for MessageLoop thread to finish"); //until thread alive while(t1.isAlive()) { try { //1 second delay and print message System.out.println("main: Continuing to wait..."); System.out.println("main: Interrupting"); t1.interrupt(); Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("main: Finished!"); } }
/////////////////////////////////////////////////////////////////////////////////////
This will product the second output:
package thread;
public class MessageLoop implements Runnable { public void run() { try { for(int i=1;i<=5;i++) { Thread.sleep(2100); System.out.println("Thread-0: Message "+(i)); } } catch (InterruptedException ex) { System.out.println("Thread-0: Message loop interrupted"); } } public static void main(String[] args) { System.out.println("main: Starting MessageLoop thread"); //create a thread Thread t1 = new Thread(new MessageLoop()); //start the thread t1.start(); System.out.println("main: Waiting for MessageLoop thread to finish"); //until thread alive while(t1.isAlive()) { try { //1 second delay and print message System.out.println("main: Continuing to wait..."); Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("main: Finished!"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
