[Solved] Confusion about interface & thread [duplicate]


Thread constructor is like

Thread t = new Thread(Runnable runn)

and not (new Runnable(){}).
When we do something as shown below

Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
});

It’s basically asking us to implements run method as defined in Runnable inteface.

Alternatively we can create a new class which implements Runnable interface and implement the run method there.

public class ThreadA implements Runnable {
    public void run() {
        // thread code goes here            
    }
}

and then we can initialize a new thread using

Thread t = new Thread(new ThreadA());

Hope this answers your doubts. Feel free to ask if any doubt exists.

1

solved Confusion about interface & thread [duplicate]