Ravissant Markandey wrote:We know that two threads can conflict only when the "runnable instance" is the same (Please correct me if im wrong, this has been my understanding so far.)
I think this view is too limited. Thread conflicts do not happen only when two threads execute the same run() method of an object implementing Runnable. It can happen anytime when several threads operate on shared data. But there definately is a possible threading conflict in your first example (because two threads execute the same method in the same object).
So in the code above do we have a SINGLE runnable instance? If not then how do we do it when we extend the Thread class?
In the second example your code must be changed to,
Thread t1=new MyThread(); // instantiate a MyThread object, not Thread
Thread t2= new MyThread();
to be meaningfull. Otherwise the run() method of MyThread won't be called at all. But maybe it was a typo?
Anyway the difference is that each thread object will call its own run() method so there's no chance of any threading conflics here. To re-establish the situation from the first example you need to do,
Thread t1=new MyThread();
Thread t2= new MyThread(t1);
The t1 thread will execute the run() method of one MyThread object. This object will then also be passed as a Runnable object (possible because Thread implements Runnable) to the t2 thread which will execute the run() method of the passed in object. So both threads now execute the same run method and you once again have a possible threading conflict on your hands.