Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Persistance In A Servlet  RSS feed

 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that starts a thread which performs a key task (sending out email messages).

Will this thread persist in memory until it is finished running, even if all users of the system ended their sessions?

Thanks,

Luke
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luke,
It depends on what is in the thread. If it has a while(true) loop, it will keep going and stay in memory until the JVM exits. I suspect this is the case with your thread.

If the thread does something and then finishes the run method, it will be garbage collected at some point.

For the future, note that we have a forum on threads & synchronization
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the thread does something and then finishes the run method, it will be garbage collected at some point.

That is true ONLY if there is no reference to the Thread being kept elsewhere. With bad design it is possible to have references to dead Threads hanging around indefinately.
Bill
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luke,
You may also want to read up on the isDaemon property of Java threads.
Long running, non-daemon threads could prevent an app server from being able to stop/reload an application properly and cause you headaches.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!