Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

destroy method and synchronization

 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know container invokes the init method once and then each client request is handled by a new thread.

Now suppose number of different threads are running then

Is it possible that container calls the destroy method when few threads are still running ?

Do we need to synchronized the destroy method if we override this method?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the destroy method and access to the destroy method is managed by the container. Besides, synchronising the method will have no effect (assuming it is called once) unless other methods in the class are also synchronised. If you are really worried you should look at read and write locks rather than mutual exclusion (mutex) locks, but I've never been bothered by the destroy method at all.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the description in the Javadocs for javax.servlet.Servlet
Hope this should clear your doubts..
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.
This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic