Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna
Created April 12, 2014 07:48
Show Gist options
  • Save rajeevprasanna/10523611 to your computer and use it in GitHub Desktop.
Save rajeevprasanna/10523611 to your computer and use it in GitHub Desktop.
package example2;
public class FooRunnable implements Runnable {
public void run() {
for (int x = 1; x < 6; x++) {
System.out.println("Runnable running");
}
}
}
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());
}
}
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();
}
}
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