Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna
Created April 12, 2014 07:57
Show Gist options
  • Save rajeevprasanna/10523858 to your computer and use it in GitHub Desktop.
Save rajeevprasanna/10523858 to your computer and use it in GitHub Desktop.
starting multiple threads
package example4;
public class ManyNames {
public static void main(String[] args) {
// Make one Runnable
NameRunnable nr = new NameRunnable();
//All three Thread instances get the same Runnable instance, and each thread is given a unique name.
Thread one = new Thread(nr);
Thread two = new Thread(nr);
Thread three = new Thread(nr);
one.setName("Fred");
two.setName("Lucy");
three.setName("Ricky");
one.start();
two.start();
three.start();
//Order of thread execution is not guaranteed.
}
}
package example4;
//Running multiple threads
public class NameRunnable implements Runnable {
public void run() {
for (int x = 1; x <= 3; x++) {
System.out.println("Run by " + Thread.currentThread().getName()
+ ", x is " + x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment