• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads in Java,can the stack with main() die/end before the stack with run()?

 
John McDowell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were to write a main class that creates a thread, is it possible that the thread created by the class outlives the main() of the class that created it.

In a way it seems possible because, I could make the newly created thread sleep for an hour , so the new stack goes into blocked state, leaving the original main stack free for execution, the main stack executes and has nothing else to do, while the new stack is still in blocked state.

But on the other hand, there is this statement in Java that everything starts and ends with the main() method.

Please let me know which one is correct
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John McDowell wrote:If I were to write a main class that creates a thread, is it possible that the thread created by the class outlives the main() of the class that created it.

Yes, that's possible unless the created thread is marked as a daemon thread.

John McDowell wrote:
But on the other hand, there is this statement in Java that everything starts and ends with the main() method.

Please let me know which one is correct

It depends on what context that statement is made.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John McDowell wrote:But on the other hand, there is this statement in Java that everything starts and ends with the main() method.


Who made this statement when?

It looks like one of the gross oversimplifications to me that some teachers make in the first lessons. So much oversimplified that it is not helpful in the long run and has to be unlearned.
 
John McDowell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hauke Ingmar Schmidt wrote:
John McDowell wrote:But on the other hand, there is this statement in Java that everything starts and ends with the main() method.


Who made this statement when?

It looks like one of the gross oversimplifications to me that some teachers make in the first lessons. So much oversimplified that it is not helpful in the long run and has to be unlearned.

Hi can you please give me a simple example of a program which doesn't start and end with main()?thanks.
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John McDowell wrote:
Hauke Ingmar Schmidt wrote:
John McDowell wrote:But on the other hand, there is this statement in Java that everything starts and ends with the main() method.


Who made this statement when?

It looks like one of the gross oversimplifications to me that some teachers make in the first lessons. So much oversimplified that it is not helpful in the long run and has to be unlearned.

Hi can you please give me a simple example of a program which doesn't start and end with main()?thanks.


Applets, only life cycle methods are called. And in my opinion every other program that runs in an container. There be a call to a main method involved somewhere to start the container, but there is none from the point of view of your program. Think of a web app running in Tomcat. Or JEE components. There are startup procedures, there are callbacks, there is configurable or conventional behaviour - but main is not used for your program. One could argue that a container managed component is not a standalone program that can execute on its own. But so is every single other Java program. They just need a different infrastructure. One could argue that it is not a "Java" program if it needs a JEE container. True. But that is the problem with the word "Java" in this context. It is overloaded with meanings: Language, JVM, ecosystem. When someone uses Spring he still is a Java programmer. Every HelloWorld that runs without any dependencies other than a standard JRE, and that is not started as an applet, and that doesn't use tricks (e.g. static block with automatic class loading triggered), needs to use the main method for its initial operations. The statement gets better when it gets more specific.

But note that I chose my words carefully. I didn't say "wrong" but "not helpful oversimplification". And the statement says: "everything starts and ends with the main method". Even a "wrong" may be justified because of the "everything".

Oh, that was all about programs that start with main. The statement also says everything ends with main. That part is plainly wrong. Start a thread in main that does a longer running operation and is not a demon thread, exit main (by not doing anything else in it), there you go. Your program executes in the thread and will return when that (last) thread finishes. No main involved.



While the behaviour is not deterministic it typically should output something like this:



As you can see in the second info block printed by the spawned thread, main is not alive anymore. It has been replaced by a thread that waits for all of your spawned threads to end to kill the VM.

I still would like to know who made this statement, and in what context.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!