aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes main thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "main thread" Watch "main thread" New topic
Author

main thread

Palash Kumar
Ranch Hand

Joined: Jul 21, 2009
Posts: 33

if main method is executed in a seperate thread....
then does it contain run()

why main thread doesnot need to be started by calling start()


SCJP 6 with 80%, going for SCWCD
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

if main method is executed in a seperate thread....


A separate thread from what?

main represents the entry point of a Stand Alone application, whereas run is really associated with a thread, and a thread is quite different from an entry point of a stand alone app.

I guess if you're wondering why, I guess they figured there's no need to.

-Cameron McKenzie
Palash Kumar
Ranch Hand

Joined: Jul 21, 2009
Posts: 33

but main is also a thread
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Well, I guess you could say a Servlet runs as a thread, as does an EJB, as does many other resources. But these resources are different enough from a thread that is spawned in a program that we treat them differently. I guess we don't need to start a thread when we call main for the same reason we don't when a servlet is invoked - it's because the runtime is just smart enough to do it for us.

-Cameron McKenzie
Andriy Pererva
Ranch Hand

Joined: Jul 19, 2009
Posts: 73
the main thread is usually started by JVM, after class-loader finished loading main class. You can try to change the signature of main method (or even delete it) and then run the class. You will get a NoSuchMethodError, because JVM didn't find an appropriate method signature.


SCJP 6.0(95%), SCWCD 5(94%), SCJD (working on B&S v.2.3.1)
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

whenever you give a class file to the JVM.....it looks for the main method in other words main method is an entry for the JVM to your code and that entry is searched in the public class as its name is same as the file.

So when the JVM enters your code as new stack for that thing materializes....if you enter something there should be some room in there to run things that it has. So a new thread for it starts with its own stack memory.

Also in threads when you run the start() method the new stack for the thread materializes.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main thread