0
package learning.java.basics1;
class Child implements Runnable{    
    Child(){
        Thread t = new Thread(this);
        t.setName("SH");
        t.start();
   }
   public void run(){
        System.out.print("child thread : "+Thread.currentThread().getName());
    }
}
public class C2 {
    public static void main(String[] args) {
        new Child();
        System.out.print("Thread name of main :"+Thread.currentThread().getName());
}

}

It prints :

Thread name of main : main
child thread : SH

So with each public class execution (here C2) JVM creates a Thread called "main" (or already existing thread with this name) and its run() method, calls our main method?

So execution step for each Java file that contains main method is as follows:

  1. JVM creates a thread called main,
  2. main(thread) calls run method
  3. run method calls directly its static method main() without creating any object.

Is it true ?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Rohit Shekhar
  • 315
  • 4
  • 15
  • Where's the picture? What kind of image is it? – Hack-R Oct 08 '16 at 19:44
  • 2
    And what is your question? – talex Oct 08 '16 at 19:45
  • Hint: ClassNames go UpperCase. Only CONSTANTS go all upper! – GhostCat Oct 08 '16 at 19:46
  • When you invoke "java" that starts a JVM; which will then run the *main* method of that class that you gave to java on the command line. One class, one main, one thread. – GhostCat Oct 08 '16 at 19:47
  • 1
    No. The JVM main thread is not created by using the Thread class nor the Runnable class. The JVM just starts a thread using native APIs and calls into your static main. See http://stackoverflow.com/questions/17432963/how-main-thread-created-by-java – Alexander Torstling Oct 08 '16 at 20:19

0 Answers0