user3081519
user3081519

Reputation: 2869

Is it wrong to do runnable.run()?

I am fairly new to the concept of multithreading and there is an aspect which is not clear to me.

There are two ways to create and run a thread:

  1. Extend a Thread class and start() the thread.
  2. Create a Runnable object, pass it to a Thread constructor and start() the thread.

And this blog post states that we always should start a thread using a start() or so was my impression of it.

But in one of the answers here you could see how a person is using runnable.run(). It gives me an impression of somewhat wrong practice.

Is it normal? Should it be avoided? You can be explicit in your answer, but any suggestions would be appreciated.

Upvotes: 4

Views: 546

Answers (1)

guest
guest

Reputation: 6698

They're just different things. run() executes the Runnable in the current thread. Calling start(), on the other hand, causes the Runnable to run in the new thread.

The tutorial points it out as a pitfall. You went through all that trouble to make a thread, so if you go on to run it in the current thread instead, then it's likely a mistake.

Upvotes: 5

Related Questions