Skip to content

Instantly share code, notes, and snippets.

@MridulS
Created December 8, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MridulS/7486898b6bb46e7ff99338a7c690f5c2 to your computer and use it in GitHub Desktop.
Save MridulS/7486898b6bb46e7ff99338a7c690f5c2 to your computer and use it in GitHub Desktop.
import java.util.concurrent.*;
import java.io.IOException;
class JumbleNames implements Runnable
{
private String firstName, secondName;
private long delay;
public JumbleNames(String firstName, String secondName, long delay)
{
this.firstName = firstName;
this.secondName = secondName;
this.delay = delay;
}
public void run(){
try{
System.out.println(firstName);
TimeUnit.MILLISECONDS.sleep(delay);
while(true){
System.out.println(secondName);
}
}
catch(InterruptedException e) {
System.err.println("Interrupted");
}
}
public static void main(String[] args){
Thread t1 = new Thread(new JumbleNames("A", "B", 100));
Thread t2 = new Thread(new JumbleNames("D", "E", 100));
Thread t3 = new Thread(new JumbleNames("F", "G", 100));
t1.setDaemon(true);
t2.setDaemon(true);
t3.setDaemon(true);
t1.start();
t2.start();
t3.start();
System.out.println("Press Enter when you had enough");
try{
System.in.read();
System.out.println("Enter pressed \n");
}
catch(IOException e){
System.err.println(e);
}
System.out.println("Ending main()");
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment