java - How does JVM know about which run method to be called while executing start() method -


in code given below have passed temp class object's reference id inside thread constructor call catched thread class's constructor in runnable type reference variable. want ask there code inside thread class constructor tells jvm particular class's run() method executed when thread created.

class temp implements runnable {    public void run()    {       system.out.println("hello thread!");    }      public static void main(string args[])      {         (new thread(new temp())).start();     } } 

inside thread.start() method, thread calls runnable.run().

a simple way of how work but really, it's not done way due example not creating new thread,

public class thread {     private runnable runnable;     public thread(runnable runnable) {      this.runnable = runnable;    }     public void start() {      if (runnable != null) {        runnable.run();      }    }  } 

then when call:

new thread(this).start(); 

a new thread created, assigning runnable inner private member. later, after object created, start() called on object, private runnable member, , call it's run() method.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -