File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Can I create threads if I don't have main(). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Can I create threads if I don Watch "Can I create threads if I don New topic

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

Waez Ali

Joined: Jan 10, 2005
Posts: 22
Is it possible to create user threads in a class that doesn't contain
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)

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Waez Ali

Joined: Jan 10, 2005
Posts: 22
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().

So my question was what if there is no main()?
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?

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

Am I correct about above fig?

waiting for your reply

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 11, 2001
Posts: 14112
Moving to the Threads forum...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Can I create threads if I don't have main().
It's not a secret anymore!