-5

we can create reference of interface but not object. but why Thread constructor accept new Runnable() that's looking like object. e.g. Thread t = new Thread(new Runnable(){});

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
user2826111
  • 152
  • 1
  • 2
  • 13

1 Answers1

0

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.

S4beR
  • 1,872
  • 1
  • 17
  • 33