Is running runnable.run () wrong?

I am new to the concept of multithreading, and there is an obscure aspect.

There are two ways to create and start a stream:

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

And this blog post says that we should always start the thread with help start()or so was my impression of it.

But in one of the answers here you could see how a person uses runnable.run(). This gives the impression of improper practice.

This is normal? Should this be avoided? You may be explicit in your reply, but any suggestions will be appreciated.

+4
source share
1 answer

These are just different things. run()executes runnable in the current thread. A call start(), on the other hand, causes Runnable to start in a new thread.

The study guide points to this as a trap. You encountered all this problem in order to create a thread, so if you continue to run it in the current thread, then this is probably an error.

+5
source

Source: https://habr.com/ru/post/1534955/


All Articles