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 runnabk interface to implement threads that

ID: 3905252 • Letter: L

Question

Lab 05 This assignment must use the runnabk interface to implement threads that represent independent singers. The snger's singing wil be simulated by displaying the each lne of the song's lyrics each of the by the singer threads. The project must have the folbwing components package threadrunnableinterfacepackage; The project ust have 3 java source Sles: Singersclass.java Contains the SingersClass class SongLyricsClass.java Contains the SongLyricsClass chss ThreadRunnableInterfaceMainClass.java Contains the ThreadRunnableInterfaceMainClass chss There are 3 java files that are inc luded in the assignment in a zpped up fokder You must use these files to develop the assigment The Java files have comments that ind cate where code has to be inserted and what the inserted code b responsible for doing to complete the assigment. In these fies, you must insert code indicated at the comments to modify the SongLyricsClass and the Thr eadRunnableInterfaceMainClass to successfuly complete the assigment. See the attached sample outputs to check that your program is behaving correctly br output. Your program output must exactly match the sample output for correct behavior and output fomatting or you wil lose poirts. The threads are nnning asynchr produce the exact results regarding the order of the threads ruming onously so difkrent platsoms that the program s run on may not Notice that the start method does not actually start the thread, but makes the thread avaiabk to be scheduld to run by the JVM. Once making the thread available to run the start) method immed atel retums to the man0 code that invoked it Note that in this program design the threads do not start running unti the main program thread ends (gives up is thread). You must inclide a couple of starvas of your favorite song yrics (not the song used in the sample output) that you choose. Use the 6 singers to test your program Do not modify the proviled 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 ");

}

}