What's the difference between different ways to start a thread?

java, multithreading

Solution

Well, you have two method to implement a multi-thread.

extend `Thread` and use `new MyThreand().start()` to start your thread.

implement `Runnable` interface. In this condition, you can use `(new Thread(new MyThread)).start()`; to start a thread.

For detailed infomation, just refer to oracle official doc.

Problem

I have a class named `MyThread` which extends the Thread class and implement the `run()` function.When I want to run it , I got two ways: - new a instance and call the function,like: `new MyThread().start()` - new a instance and transmit the instance to the construction function of Thread as a parameter and then call the start function of Thread. Like this: `(new Thread(new MyThread)).start();` Anybody can just tell the difference?

Original source

Related problems