1

In java for example a thread can be created using either implementing Runnable or extending Thread class. I do understand extending class but by implementing interface how does JVM decide what to attach with it. Is it that such implementation is purely a implicit mechanism or I am missing something here.

Edit: Same concept is while we create objects as Map map=new HashMap(), than how does it maintain that it is containing hashmap object. Is the info stored in object or a reference. How JVM looks at it?

Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27
  • duplicate of: http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread – Ahmed KRAIEM Aug 26 '13 at 09:10
  • `class Thread implements Runnable`... – Anders R. Bystrup Aug 26 '13 at 09:16
  • I know that but what is happening in JVM..the link suggested is more on use and difference. – Pankaj Sejwal Aug 26 '13 at 09:19
  • 1
    @Blackbeard, this is actually a really great question. Unfortunately, by asking specifically about threads, you've got people talking about threading. But your question, if i understand it right, is completely general - you want to know how the JVM invokes the methods in the right class when it has a variable of some interface type (like `Map`/`HashMap`). The fact that it can do this is absolutely central to object-oriented programming, so it's a wise question to ask! – Tom Anderson Aug 26 '13 at 10:38
  • 1
    Sadly, i am not going to give you a proper answer (i'm too lazy), but the key think you should know is that every object in memory contains an indication of what class it is. So the JVM can ignore the type of the variable the reference happens to be stored in, and look inside the object to find what class it actually is. From there, it can work out what method to run. This is called *dynamic dispatch*, and it is a vital part of *polymorphism*. The actual implementation of this is quite complicated - you will have to dive in to assembly language and JIT internals to understand it. – Tom Anderson Aug 26 '13 at 10:44
  • @TomAnderson: thanks for info, I was looking towards something like this. I have some hand on JIT but wanted to be more sure if some info is handy on it. – Pankaj Sejwal Aug 26 '13 at 11:46

2 Answers2

1

Is it that such implementation is purely a implicit mechanism or I am missing something here.

A thread is not implicitly created just by the act of implementing Runnable. An instance of such an object must be explicitly passed into the Thread constructor:

new Thread(myRunnable);

Clearly, the Thread instance will break no sweat figuring out which Runnable to call.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

If You can see the source code of Thread class you can understand that even Thread class itself implements Runnable interface.

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Thread.java#Thread.run%28%29


public More ...Thread() {
  init(null, null, "Thread-" + nextThreadNum(), 0);
}

public More ...Thread(Runnable target) {
  init(null, target, "Thread-" + nextThreadNum(), 0);
}

public synchronized void More ...start() {
if (threadStatus != 0)
  throw new IllegalThreadStateException();
  group.add(this);
  start0();
  if (stopBeforeStart) {
         stop0(throwableFromStop);
  }
}

private native void More ...start0();

public void More ...run() {
  if (target != null) {
       target.run();
  }
}

So, Case:1 i.e. extending Thread class Here basically you will be overriding the run() method of Thread.class and hence on invoking the start(), the native start0() method creates the new Thread from underlying OS and invokes the overridden run() method.

Case:2 i.e. implementing Runnable Interface Here you will be setting the Runnable implementation object to target. Hence on invoking the start(), the native start0() method creates the new Thread from underlying OS and invokes the run() method of the Thread.class and as you can see it will call the target.run() i.e. your Runnable implementation Object's run() method.

Sandy
  • 972
  • 5
  • 15
  • 28