Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 05 This assignment must use the runnabe interface to implement threads that

ID: 3909536 • Letter: L

Question

Lab 05 This assignment must use the runnabe interface to implement threads that represent independent singers The singer's singing will be simulated by displaying the each line of the song's lyrics each of the by the singer threads. must have the following components: The project package threadrunnableinterfacepackage: The project must have 3 java source fles: SingersClass.java Contains the Singerlass class SongLyricsClass.java Contains the SongLyricsclass class ThreadRunnableInterfaceMainclass.java Contains the ThreadRunnableInterfaceMainclass class There are 3 java files that are included in the assignment in a zipped up folder. You must use these files to deveop the assignment. The Java fies have comments that indicate where code has to be nserted and what the inserted code is responsible for doing to compkete the assignment. In these es, you must insert code indicated at the comments to modify the SongLyricsclass and the ThreadRunnableInterfaceMainclass See the attached sampke outputs to check that your program is behaving correctly for output. Your program output must exactly match the sampk output for correct behavior and output formatting or you will lose points. The threads are running asynchronously so diflerent platforms that the program is run on may not produce the exact results regarding the order of the threads running. Notice that the start0 method does not actually start the thread, but makes the thread available to be scheduled to run by the JVM. Once making the thread available to run, the start) method immedately returns to the main code that invoked t.Note that in this program design, the threads do not start running unti the main program thread ends ( gives up its You must include a couple of stanzas of your favorite song lyrics (not the song used in the sample output) that you choose. Use the 6 singers to test your program. Do not modify the provided program to use any prompts.

Explanation / Answer

//SingerClass.java

package threadRunnableInterfacePackage;

/***

* singer class have run method

*

*/

public class SingerClass implements Runnable {

//run method

@Override

public void run() {

SongLyricsClass obj = new SongLyricsClass();

for(int i=0;i<obj.getNoOfLine();i++){

System.out.println(obj.getLyrics(i));

}

//after exiting the loop printing the message

System.out.printf("%s Done ",Thread.currentThread().getName());

}

}

//SongLyricsClass.java

package threadRunnableInterfacePackage;

public class SongLyricsClass {

//declared the synchronized method

public String lyrics[][]=new String[2][1];

//synchronized method

//return the lyrics by line no

public synchronized String getLyrics(int lineNo){

//song lyrics

lyrics[0][0] = "When I was young, they told me, they said";

lyrics[1][0]="Make your bed, you lie in that bed";

return lyrics[lineNo][0];

}

//get line method

public int getNoOfLine(){

return lyrics.length;

}

}

//ThreadRunnableInterfaceMainClass.java

package threadRunnableInterfacePackage;

public class ThreadRunnableInterfaceMainClass {

public static void main(String[] args) {

//created 6 method of singer class

SingerClass singer1 = new SingerClass();

SingerClass singer2 = new SingerClass();

SingerClass singer3 = new SingerClass();

SingerClass singer4 = new SingerClass();

SingerClass singer5 = new SingerClass();

SingerClass singer6 = new SingerClass();

//created six threads and passed the parameter

//of above singer class

//and starting the thread

Thread t1 = new Thread(singer1,"Singer 1");

t1.start();

Thread t2 = new Thread(singer2,"Singer 2");

t2.start();

Thread t3 = new Thread(singer3,"Singer 3");

t3.start();

Thread t4 = new Thread(singer4,"Singer 4");

t4.start();

Thread t5 = new Thread(singer5,"Singer 5");

t5.start();

Thread t6 = new Thread(singer6,"Singer 6");

t6.start();

//waiting to complete above threads

try{

Thread.sleep(3000);

}

catch(InterruptedException e){

System.out.println(e.getMessage());

}

//printing the message after

System.out.println("Program Done ");

}

}