-7

Why we we use Runnable interface even it has no connection with start() method? Why we can't just write run() method and start?

Why we need to implement run() method,instead of using it directly and start process using start() method?

azro
  • 53,056
  • 7
  • 34
  • 70
Vignesh
  • 7
  • 3
  • Possible duplicate of [What does it mean to "program to an interface"?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – khelwood Jan 15 '18 at 09:09
  • @khelwood: Actually I think it's more basic than that. :-) – T.J. Crowder Jan 15 '18 at 09:10
  • 2
    Your question is a bit vague. Can you show what you would want to write instead of using Runnable? – Thilo Jan 15 '18 at 09:13
  • 2
    There is a connection between `run` and `start`: The thread that was just created by `start` immediately calls `run`. – Thilo Jan 15 '18 at 09:15
  • Good point, I think 'run' is a bit of a misnomer. That method should have been called 'onStart()' or something. It's not a method you call yourself, but rather something you have to implement. And the Thread controls it's lifecycle. – Axel Podehl Jan 15 '18 at 09:15
  • The `Runnable` interface does not have a `start` method. – DodgyCodeException Jan 15 '18 at 09:47

1 Answers1

9

If you just called the run method directly, it would run on the thread you used to call it. By implementing Runnable and passing your instance into new Thread, you're setting it up so that run will be called on the new thread.

I recommend working your way through the Java Concurrency tutorial, which will go into creating and running threads in detail.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875