I have recently started digging into Multi-Threading in Java. While exploring things, I discovered there are 2 quick and dirty ways to create Threads "on the go" in Java. Here is the example:
public static void main(String[] args) {
System.out.println("Thread: " + Thread.currentThread().getName());
new Thread() {
public void run() {
System.out.println("Thread: "
+ Thread.currentThread().getName());
System.out.println("hello");
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread: "
+ Thread.currentThread().getName());
System.out.println("hello");
}
}).start();
}
- First one is the one which starts with
new Thread() {
- Second one comes after that which starts with
new Thread(new Runnable() {
I just wanted to ask if both of the ways are correct? Any difference other than difference of Implementing Runnable Interface v/s Extending Thread class?