-1

When I searched on the internet about extending the Thread class, all the examples I came across had overridden the run() method. I have also extended the Thread class in my program but I haven't defined the run() method. It's just another class (I'll refer to it as MyClass), which extends Thread class, with a constructor and a few methods defined by me. However, I'm puzzled because I have created an object of MyClass in the Main class and have called the start() method using that object in main() method. And it still works.

My question is whether the run() method is implicitly defined by the Java compiler, like the default constructor, or is it simply not required to be overridden when extending the Thread class?

Deepak
  • 33
  • 9
  • 3
    What purpose does extending `Thread` have if you don't override `run`? What are you adding that's useful? (Note: In general, extending `Thread` even if you do override `run` is generally second-best to implementing `Runnable` separately and passing it into `new Thread`.) – T.J. Crowder Sep 10 '18 at 17:56
  • @T.J.Crowder I have created a class which handles the database (insertion, deletion, etc.). And I need it across my various activities in different packages. So, instead of creating this class' object in each activity and re-establishing a connection to the database again and again, I've extended the Thread class to the database class and have created its static final object in my main class, which runs in the background in a separate thread, and I'm using it across all other activities. So, I have no work to give to the `run()` method. – Deepak Sep 10 '18 at 18:14
  • It sound like you're not overriding `run` and also not providing a `Runnable` to the `Thread` constructor. If that's true, you're not using `Thread` correctly. Making a subclass of `Thread` and adding methods to it does nothing whatsoever to make the code in those methods run on another thread. – T.J. Crowder Sep 10 '18 at 18:18
  • Not even if I do `object.start()` ? – Deepak Sep 10 '18 at 18:20
  • Not even if you call `start`. :-) See [the documentation](https://docs.oracle.com/javase/10/docs/api/java/lang/Thread.html#start()) and [the tutorial](https://docs.oracle.com/javase/tutorial/essential/concurrency/). All calling `start` does is start the thread and cause its `run` method to be called on the newly-started thread. If you don't override `run` provide a `Runnable`, the default `run` will return immediately, and the thread will stop. It has no effect, at all, on what thread the subclass's methods run on. – T.J. Crowder Sep 10 '18 at 18:26
  • I see. So only the block of code that is within the `run()` method will run in a separate thread while all other methods outside `run()` won't? Then I think I don't have any use of Thread for my program :/ I can't give `run()` any work. I'm just using the class' methods in other activities. The class, by itself, does nothing. – Deepak Sep 10 '18 at 18:32
  • @T.J.Crowder If I establish the database connection in the `run` method (instead of the constructor), the connection will remain intact even after the thread dies, right? – Deepak Sep 10 '18 at 18:54
  • @T.J.Crowder, I think you should say, "...cause its run method to be called _by_ the newly-started thread." Virtually all newbies are taught that functions _do_ things. But really, that's a white lie. The newbies won't fully grasp what multi-threading means until they understand that a function is just a static, sequence of instructions, and everything that gets _done_ is done by threads, executing those instructions. – Ohm's Lawman Sep 10 '18 at 20:50
  • @Deepak - There's no point in establishing the connection in `run` if you're just going to let the thread immediately die. Most advice I can find says don't share a connection between threads (ex. [here](https://stackoverflow.com/a/7188111/157247), [here](https://stackoverflow.com/a/39067701/157247)), but it looks like with **some** JDBC providers it's possible, if you're careful (ex. [Java DB](https://docs.oracle.com/javadb/10.8.3.0/devguide/cdevconcepts23499.html)). I would have one connection per thread, and only use it on that thread. Happy coding! – T.J. Crowder Sep 11 '18 at 06:32
  • Okay. Thank you. – Deepak Sep 12 '18 at 20:23

1 Answers1

2

No, the run() method is not implicitly defined by the Java compiler. It is explicitly defined by the Thread class.

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

It is not abstract, so it does not need to be overridden. There is nothing special about the run method here.

Because you're extending Thread and not supplying a separate Runnable, your class inherits the run method; run runs and does nothing.

Because it does nothing, it's not very useful. Usually you want to do something in your Thread, so normally when creating a Thread this way, run is overridden to do something useful.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • (Copied from my other comment on the question) I have created a class which handles the database (insertion, deletion, etc.). And I need it across my various activities in different packages. So, instead of creating this class' object in each activity and re-establishing a connection to the database again and again, I've extended the Thread class to the database class and have created its static final object in my main class, which runs in the background in a separate thread, and I'm using it across all other activities. So, I have no work to give to the run() method. – Deepak Sep 10 '18 at 18:17