4 Replies Latest reply: Jan 18, 2010 11:54 AM by 3004 RSS

    Threads:Runnable Interface's run() method.

    843789
      I know that an interface contains only method declaration and no body.

      I read that a thread starts it work when it encounters the run() method.
      If run() is a method in an interface named Runnable, how does the compiler get to know that the function implies that the thread is supposed do some work.
      Because the run() function that you are overriding does not have any body or particular functionality, how does the compiler understand that the run() method has the logic for executing the work assigned to the thread.
      Has the logic for the run() method prewritten somewhere?
        • 1. Re: Threads:Runnable Interface's run() method.
          843789
          I read that a thread starts it work when it encounters the run() method.
          Wrong. A thread will run the code in the run method, but to start the thread, you must call start()

          Thread will run its own run method, or the run method of the runnable if it is set up with a Runnable
          • 2. Re: Threads:Runnable Interface's run() method.
            843789
            Thanks, but this is not exactly what I was expecting.
            What I meant to ask was something related to interfaces and the methods contained in them.

            Suppose I am writing an interface called MyInterface and it has some method like method1(). When I extend MyInterface to some class, then I override the functionalty for method1(). I can write some logic for adding two numbers or printing something etc.

            But when you implement Runnable and override the run() method, the compiler gets to know that the particular piece of code inside run() method is the work that is assigned to the thread.

            Eventhough I am not explicitly writing any logic for the run() method, How is this possible?
            I hope you guys get my point.

            Edited by: Blueray2009 on Jan 18, 2010 9:14 AM
            • 3. Re: Threads:Runnable Interface's run() method.
              843789
              Blueray2009 wrote:
              Suppose I am writing an interface called MyInterface and it has some method like method1(). When I extend implement MyInterface to some class, then I override implement the functionalty for method1(). I can write some logic for adding two numbers or printing something etc.

              But when you implement Runnable and override implement the run() method,
              the compiler gets to know that the particular piece of code inside run() method is the work that is assigned to the thread.
              Eventhough I am not explicitly writing any logic for the run() method
              Huh?

              The JVM knows because
              the class that implements the run() method
              is what you pass to the Thread constructor.
              class Blueray {
                  public static void main(String[] arg) {
                      new Thread(new Runnable() {
                          public void run() {
                              System.out.println("In "+this.getClass().getName()+".run()");
                          }
                      }).start();
                  }
              }
              • 4. Re: Threads:Runnable Interface's run() method.
                3004
                Blueray2009 wrote:
                Suppose I am writing an interface called MyInterface and it has some method like method1(). When I extend MyInterface to some class, then I override the functionalty for method1(). I can write some logic for adding two numbers or printing something etc.

                But when you implement Runnable and override the run() method, the compiler gets to know that the particular piece of code inside run() method is the work that is assigned to the thread.

                Eventhough I am not explicitly writing any logic for the run() method, How is this possible?
                It doesn't matter if it's the run() method, or any other method in an interface, or an abstract method in a class, or a concrete method that's been overridden. It's all the same, and it's the definition of how runtime polymorphism works in Java.
                ParentType p = someConcreteImplementation(); // e.g. Runnable = new MyRunnable();
                p.executeOverriddenOrImplementedMethod9);
                The compiler knows that p is a reference to ParentType and that the parent type defines the method. It also knows that whatever appears on the RHS of the equals sign MUST refer to a concrete class that has a concrete implementation of that method. It knows this because the JLS defines it to be so, and the compiler's job is to implement the rules defined in the JLS.

                At runtime, the JVM has a reference to that concrete object, and again, because by definition the rules say it must, it executes the implementation of that method that's in that class, or in its nearest ancestor that implements or overrides it.