Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:45:12+00:00 2026-05-27T02:45:12+00:00

In my servlet I start a background thread onContextInitialized() and consequently I need to

  • 0

In my servlet I start a background thread onContextInitialized() and consequently I need to stop it onContextDestroyed(). So my servlet needs a private instance variable (let’s call this daemon) pointing to this daemon thread.
The daemon runs fine, but the reference is lost as soon as I exit contextInitialized()! For example, if I implement doGet() to show the status of the thread, it throws a NPE

So the answer is, who can access my servlet private instance variable and set it to null?
I remark that the thread runs just fine, just the reference is broken

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:45:13+00:00Added an answer on May 27, 2026 at 2:45 am

    Just set the reference in Application Context or set it to some static field of some class[less preferable]

    Not sure what you want to achieve but this will do

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a servlet, which must start a thread in init() and correctly
I need to launch a background task from a servlet in a GlassFish server,
I would like to start a thread on handling the multipe Form, one Servlet
How can I start a thread which will run in the background but the
I have one global variable in servlet context, when application start up, I will
am I allowed (without any sideeffects) to create and start a new Thread() from
I'd like to start a long running task on the Backend instance. This backend
I start h2 database in a servlet context listener: public void contextInitialized(ServletContextEvent sce) {
When does a Java Filter start? Does the Filter init() method overide the servlet
Let me start by pointing out that while I've been using Java SE for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.