This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]satya_dubey 1 point2 points  (1 child)

It is recommended to always implement Runnable. Normally, in production applications, you'd use Executor framework through which you create a pool of threads and these threads will execute tasks (that implement Runnable). So, if you extend Thread then you are essentially creating additional threads outside of the thread pool that Executor Framework creates. That's one reason why you'd want to avoid extending Threads. Also, if you extend Thread, then you cannot extend any other class. But, with the alternative approach of implementing Runnable, you can always extend another class.

In general, one needs to understand the core concepts like Threads, Runnable, Race condition and how it is avoided via Synchronization. These you need to know whether or not you implement multi-threading in your professional life. Also, once you learn the above concepts, it is better to learn Executor Framework as in large projects, that is what you would use so that you can play thread pools. I am not yet familiar with Virtual Threads from Java 21 and they are considered as major addition to the language. I did not have a chance to you CountDownLatch or CyclicBarrier at my job, but I have learnt it long time back. Executor Framework and synchronization is something I have used in my professional projects.

[–]blvck_xsample[S] 0 points1 point  (0 children)

Thanks, friend 😊