Created
April 12, 2014 08:23
-
-
Save rajeevprasanna/10524505 to your computer and use it in GitHub Desktop.
Thread sleep
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 example5; | |
public class ManyNames { | |
public static void main(String[] args) { | |
// Make one Runnable | |
NameRunnable nr = new NameRunnable(); | |
Thread one = new Thread(nr); | |
one.setName("Fred"); | |
Thread two = new Thread(nr); | |
two.setName("Lucy"); | |
Thread three = new Thread(nr); | |
three.setName("Ricky"); | |
one.start(); | |
two.start(); | |
three.start(); | |
// Just keep in mind that the behavior in the preceding output is still | |
// not guaranteed. You can't be certain how long a thread will actually | |
// run before it gets put to sleep, so you can't know with certainty | |
// that only one of the three threads will be in the runnable state when | |
// the running thread goes to sleep. I | |
} | |
} |
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 example5; | |
class NameRunnable implements Runnable { | |
public void run() { | |
for (int x = 1; x < 4; x++) { | |
System.out.println("Run by " + Thread.currentThread().getName()); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException ex) { | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment