Created
April 12, 2014 07:48
-
-
Save rajeevprasanna/10523611 to your computer and use it in GitHub Desktop.
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 example2; | |
public class FooRunnable implements Runnable { | |
public void run() { | |
for (int x = 1; x < 6; x++) { | |
System.out.println("Runnable running"); | |
} | |
} | |
} |
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 example3; | |
class NameRunnable implements Runnable { | |
public void run() { | |
System.out.println("NameRunnable running"); | |
//Invoked the static Thread.currentThread() method, which returns a reference to the currently executing thread, and | |
//then we invoked getName() on that returned reference | |
System.out.println("Run by " + Thread.currentThread().getName()); | |
} | |
} |
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 example3; | |
public class NameThread { | |
public static void main(String[] args) { | |
NameRunnable nr = new NameRunnable(); | |
Thread t = new Thread(nr); | |
//Even if you don't explicitly name a thread, it still has a name. | |
//Run this class be commenting below setName call | |
t.setName("Fred"); | |
t.start(); | |
} | |
} |
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 example2; | |
public class TestThreads { | |
public static void main(String[] args) { | |
FooRunnable r = new FooRunnable(); | |
Thread t = new Thread(r); | |
t.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment