user3239652
user3239652

Reputation: 785

questions regarding multi threading

  1. Thread class is said to have a "IS-a" relation as well as "Has-a" relation with the runnable interface. What is the benefit of having both of these?

  2. Why do we give priority to "Has-a" relation?

Upvotes: 1

Views: 64

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503869

What is the benefit of having both of these?

It was a poor design choice, IMO. It would have been cleaner to avoid Thread implementing Runnable in the first place. This has led to various bugs (as witnessed by questions on Stack Overflow) where people have called run on a Thread instead of start.

Why do we give priority to "Has-a" relation?

Composition is generally more flexible than inheritance. If you create a Runnable implementation you can pass that to an ExecutorService instead of a Thread, for example. You aren't tying yourself to one particular way of executing the action - you're just saying "this is the code I want to run."

Upvotes: 5

Related Questions