• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Infinite While Loop, during Tomcat Startup

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: https://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.
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your support. I managed to understand the solution.
 
reply
    Bookmark Topic Watch Topic
  • New Topic