Lab 05 Threads Runnable Interface This assignment must use the runnable interfac
ID: 3865511 • Letter: L
Question
Lab 05 Threads Runnable Interface This assignment must use the runnable interface to implements threads that represent singers. The singer's singing will be simulated by displaying the each line of the songs lyrics. The project must have the following components: package threadRunnablelnterfacePackage SingersClass.java SongLyricsClass.java ThreadRunnablelnterfaceMainClass.java In the SingersClass.java: Implement the run method that represents each singer. In this method design a loop that the singer uses to sing the song. Create a class for the song lyrics. SongLyricsClass.java Have a Sting array that has the song's lyrics. Have an accessor synchronized get method to get each line of the song array by requesting the line no. Have an accessor get method to get the total number of lines in the song. You must include a couple of stanzas of your favorite song lyrics that you choose. ThreadRunnablelnterfaceMainClass.java Use spate 6 singers to test the program. In a loop, have each singer start singing. Display a message when the loop is done. Display a message when the main program is done. For a reference solution to help you with the program development, see the jpeg files.Explanation / Answer
Code:
ThreadRunnableInterfacePackage.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package threadrunnableinterfacepackage;
/**
*
* @author miracle
*/
public class ThreadRunnableInterfacePackage {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Thread Singer1=new Thread(new SingerClass("Singer1"));
Thread Singer2=new Thread(new SingerClass("Singer2"));
Thread Singer3=new Thread(new SingerClass("Singer3"));
Thread Singer4=new Thread(new SingerClass("Singer3"));
Thread Singer5=new Thread(new SingerClass("Singer3"));
Thread Singer6=new Thread(new SingerClass("Singer3"));
Singer1.start();
Singer2.start();
Singer3.start();
Singer4.start();
Singer5.start();
Singer6.start();
System.out.println("Loop completed");
System.out.println("Main program completely executed");
}
}
SongLyricsClass.java
class SongLyricsClass {
public SongLyricsClass() {
public String arr[]={"This","array","contains","each line of lyrics","of","your","desired","Song"};
public String getLyricsByLine(int lineNumber){
return arr[lineNumber+1];
}
public int getTotalLines(){
return arr.length;}
}
}
SingerClass.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package threadrunnableinterfacepackage;
/**
*
* @author miracle
*/
class SingerClass implements Runnable{
public String singerName;
SingerClass(String name){
singerName=name;
}
SongLyricsClass song=new SongLyricsClass();
public void run()
{
System.out.println("Singer is"+singerName);
for(String lyrics:song.arr){
System.out.println(lyrics+"");
}
System.out.println("Singer"+singerName+"Stopped");}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.