Reputation: 7844
I wanted to write multiple threads under one class and found one way of doing it.
public class ThreadExample {
public static void main(String[] arg)
{
Thread one = new Thread() {
public void run() {
try {
Thread.sleep(2000);
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("One");
}
};
Thread two = new Thread() {
public void run() {
System.out.println("Two");
}
};
one.start();
two.start();
}
}
Thing I don't get here is, I am neither extending the Thread
class nor am I implementing the Runnable
interface. How do I understand this?
And If the answer is, "I am just creating the object of the Thread
class and using it, then why not always do this rather than doing the things mentioned above?
Upvotes: 0
Views: 62
Reputation: 425208
Ah, but you are extending the Thread
class... anonymously.
When using the new Class() {}
syntax, you are creating an anonymous class that is a sub-class (ie effectively extends
) of the named class.
In this case, you have overridden the Thread.run()
method.
This approach, while it works, it not considered "good design", because you are extending Thread, but you are not creating a new kind of Thread. It is better to pass a Runnable
into the constructor:
new Thread( new Runnable() {
public void run() {
// do something
}
}).start();
Upvotes: 2
Reputation: 12443
Bohemian answered your first question so I'll answer your second one: "I am just creating the object of the Thread class and using it, then why not always do this rather than doing the things mentioned above?"
If you always do it the way you did in your question then you can't separate the logic that performs the task from the way the task is run. Implementing runnable
lets you bundle the logic that performs your task separately from the code that manages how your task is run.
If you use runnables you are able to run your task in a new thread, run your task in the calling thread, run your task after some delay or run it in some other fashion without modifying the code in the runnable.
When working with threads you'll generally want to use an ExecutorService instead of using the thread class directly. Executor services provide facilities for limiting the numbers of threads, handling failed executions, determining when a runnable completes, getting return values from tasks run in other threads and so on.
Upvotes: 1
Reputation: 10843
Technically, what you are doing is extending Thread
. You are extending it on the fly with something called an anonymous inner class - to learn more about what that is, start here and read this page plus the next two.
What this means is you are creating temporary inline subclasses that have no name, and whose definitions survive only until the method finishes. Why do this rather than create a named subclass of Thread
or implementation of Runnable
? It makes sense when the body of run()
is as simple as the examples above - there's less typing and fewer .java
files to keep track of. One-off simple tasks like these are good candidates for anonymous inner class extension of Thread
. For major program logic, you probably want to do a full named class implementation in a separate file.
In addition, once you've gotten good experience with Thread
, I would encourage you to check out the java.util.concurrent
package.
Upvotes: 2