Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

No appreciable difference if you create anonymous classes as you have provided in the question.

However difference between Thread t = new SubClassOfThread(); and Thread t = new Thread(new ClassImplementingRunnable) is that

since SubClassOfThread extends Thread you cannot use it to extend any other class where as in case of Runnable you can make it extend one more Class.

I prefer the second way because it decouples the Runnable from the Thread.

The first way creates a new anonymous class that extends Thread. But you only should extend another class in the sense of "is a". In your example this would be ok, but if the responsibility of the run method grows, it would be cleaner to say that your function is Runnable, but not that its main purpose is to be a Thread.

The best practice states that you should generally favor composition over inheritance. Applied to your question, your first example makes a subclass of Thread and overrides its run method (inheritance), and the second example instantiates the standard Thread class, passing it a Runnable with the run method implemented as needed (composition).

Why the latter is a best practice comes to light when you manage to inadvertently involve a member of Thread (a method or field) in your run method due to a typo or other oversight. Once that happens, you'll have a ride finding the cause of possibly very weird behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top