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(){});
Asked
Active
Viewed 71 times
1 Answers
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
-
Thanks so much.... i thinks it's enough for me – user2826111 Sep 28 '13 at 14:23