Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Infinite While Loop, during Tomcat Startup  RSS feed

 
Robert Insanovation White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is bad design, but I want to feed my curiosity and I want to obtain my goal without using any framework.

I have a Java Context Listener class, which creates a new object as a Thread.

Listener Class:



And the other Class:




The problem is that Tomcat doesn't deploy completely nor starts the web application, but it creates that object, which consists of an infinite while loop.

(A similar question has been addressed here: http://www.coderanch.com/t/233736/threads/java/Servlet-Running-Infinite-loop )


I need to understand what would the smallest modifications be, in order for Tomcat to start completely, while the infinite loop would continue to execute.
 
Tim Holloway
Bartender
Posts: 18705
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why Tomcat wouldn't finish starting up, but there are 2 things worth noting.

1. You've constructed the Thread object and assigned it to a method variable. That variable will go out of scope when the contextInitialized() method exits. Since the thread is self-sustaining, however, it will not be garbage-collected (until it terminates).

2. The fact that it's an infinite loop means that it WON'T ever terminate. Tomcat is an Application running in a JVM. There's actually nothing magic going on there, just a complex program that listens to ports and responds to them along with various support functions. But that's beside the point. The point is that no JVM can ever shut down until all Threads spawned within that JVM have been shut down. Since your thread contains an Infinite loop, Tomcat can only be halted by brute-force termination of the OS process running the JVM.

A thread should always have a way to terminate itself. The thread.cancel() method that allows other threads to terminate a thread is long-since deprecated and should not be used.
 
Robert Insanovation White
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your support. I managed to understand the solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!