Created
April 12, 2014 07:57
-
-
Save rajeevprasanna/10523858 to your computer and use it in GitHub Desktop.
starting multiple threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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