top of page

"Running with the Best: Why Runnable Outshines Thread Class for Concurrency in Java"

Writer's picture: Hiren DesaiHiren Desai

The most common difference is

  • When you extends Thread class, after that you can’t extend any other class which you required. (As you know, Java does not allow inheriting more than one class).

  • When you implements Runnable, you can save a space for your class to extend any other class in future or now.

However, the significant difference is.

  • When you extends Thread class, each of your thread creates unique object and associate with it.

  • When you implements Runnable, it shares the same object to multiple threads.


ThreadVsRunnable.java




Output:










In the Runnable interface approach, only one instance of a class is being created and it has been shared by different threads. So the value of counteris incremented for each and every thread access.


On the other hand, Thread class approach, you must have to create separate instance for every thread access. Hence different memory is allocated for every class instances and each has separate counter, the value remains same, which means no increment will happen because none of the object reference is same.


Overall, both approaches are valid and have their own advantages and disadvantages. It's up to you to decide which one to use based on your specific requirements and design goals.




32 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Comments


bottom of page