I've seen several different ways to make a new Thread, but there's one way I've seemingly forgot about, and can't find many examples of it, and I'd like to compare it to another way:
This one I've seemed to forgotten about, I'm not sure if it requires to implement Runnable or not:
new Thread()
{
public void run()
{
System.out.println("running");
}
};
vs.
new Thread(new Runnable()
{
public void run()
{
System.out.println("Running");
}
});
Differences? Advantages disadvantages?
and when should I make an anonymous Thread, vs when to implement Runnable?
I just saw, you accepted an answer, there after also I could not resist myself to provide my answer here.
There is no sign of Thread
subclass in your question, so there is noting to do with extending Thread
or implementing Runnable
, here.
Here you are just creating Thread
object in two different manners, by using two different constructors. In the second case, you are using this version of constructor for creating a new Thread
instance. You are basically providing an external Runnable
object to run its run method, when your instantiating thread runs.
Here is the source code of run
method, here you will see that in the, run
method of Thread
, target
's run
method is called, in case you provided it!
There is nothing to do with the performance. Which constructor will you use, is a matter of your use case. In simplest case, we generally do not need or use the second one.
For resolving any confusion, you can just go through the source code!