File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I create threads if I don't have main().

 
Waez Ali
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it possible to create user threads in a class that doesn't contain
main().
My understanding about threads is,main thread is just to serve user threads now if I have class that doesn't have main()....(eg. Applets)
Can i make any of my user thread as daemon or main thread so that they could serve remaining threads.
(if yes then Please provide sample code)


Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method main() has no special relationship at all with Threads. Any method, on any Thread, can create a new Thread (unless prohibited by the security policy, of course.)

Virtually every single one of the Sun demo applets that comes with the JDK creates one or more Threads.
 
Waez Ali
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
Actually I read in khalid mughals book that

* Daemon Threads exist to serve child threads.
* First child thread that jvm creates when app starts is called as main thread.This Thread executes main().

-------------------Question?---------------------------------------------
So my question was what if there is no main()?
PLEASE EXPALIN ME WHAT DOES MAIN THREAD DO IN THIS SITUATION?
------------------------------------------------------------------------
And I also read

"Calling setDaemon(boolean) method in the Thread class marks the current thread as Daemon Thread or Child Thread(main Thread),but this must be done before Thread is started"

I am not getting
----------------------------------------------------------------------
how can we create Daemon Thread or Main Thread using that function?
PLEASE PROVIDE ME SAMPLE CODE....
----------------------------------------------------------------------

There can be only one Daemon Thread and one Main thread M I RIGHT?
like
Daemon
|
Main
__|__
| | |
t1 t2 t3

Am I correct about above fig?

waiting for your reply

Thanks.
 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ernest said, the main() method has no relation to the name of the "main" Thread created by the JVM. When you start the JVM, you always provide the name of a class. That class must have a main() method. The JVM starts many threads when it starts up. One or more are for garbage collection, and one is for running your program. The latter one is called "main" simply because it's the main thread for your program. It could just as easily have been called "bob."

Once that thread starts, it calls the main() method of the class you specified -- not because it has the same name but rather because that's what the JVM specification says to do. The main() method is the entry point of your program.

Your diagram is incorrect. The main thread is started by the JVM and is not a child thread of any other thread that I'm aware of -- certainly not a daemon thread. The JVM will continue running as long as there is at least one non-daemon thread still running. Thus, you mark a thread daemon when you don't want it to keep the JVM from exiting.

I recommend that you start writing little test programs to become familiar with how these concepts operate. You can continue asking questions here, but until you "get your hands dirty" the answers will not sink in. Sometimes you simply have to roll up your sleaves and write code to see how things work.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Threads forum...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic