DEV Community

loading...

Discussion on: Runnable vs Threads

Collapse
thorstenhirsch profile image
Thorsten Hirsch

Well, the reason why in your last example the runnable threads are operating on the same object is because you only have instantiated one object, but you're using it in both threads:

        Runnable myRunnable = new MyRunnable();
        Thread myThread = new Thread(myRunnable);
        Thread myThread2 = new Thread(myRunnable);
Enter fullscreen mode Exit fullscreen mode

If you want independent objects in your threads you can do that with runnable as well:

        Thread myThread = new Thread(new MyRunnable());
        Thread myThread2 = new Thread(new MyRunnable());
Enter fullscreen mode Exit fullscreen mode
Collapse
oshanoshu profile image
Oshan Upreti Author

That's absolutely right. Thank you for your observation.
The point I wanted to prove here was that we can't create threads extending the Thread class that operates on the same object. And, that's the advantage of using the Runnable interface over the Thread class.