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

  • Home
  • SEARCH
  • 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 6559991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:24:08+00:00 2026-05-25T13:24:08+00:00

I have a daemon thread which is started when a page is opened. The

  • 0

I have a daemon thread which is started when a page is opened. The thread is then stopped when the page is closed. So in my class which holds the thread, I have it created like this:

class A {
 private static volatile boolean isStopped=false;

 //this method is called then the page is loaded
 public void testListener() {
   Thread listener = new Thread(new Runnable() {
      public void run() {
       while(!isStopped) {
        //perform listener event
       try {
         //after every event sleep for a while
         Thread.sleep(1000 *2)
       } catch(InterruptedException e){}
      }
     }
    });
 }
 listener.setName("Test-Server-Daemon");
 listener.setDaemon(true);
 listener.start();

 // reset back to false so thread can be restarted when the page load event,
 // call this method instance
 if (isStopped) {
   isStopped=false;
 }
}

 /**This is called when page is closed**/
 public static void stopListener() {
   isStopped=true;
  }
}

Upon investigation, I have noticed that when the page is closed and not opened again within say 30 seconds interval, the thread is gracefully stopped.

But when the page is closed and re-opened within say 2 seconds interval the old thread does not get stopped and hence runs simultaneously with a new one.

And so as you can see from below image, I have the same thread started again when I close and quickly open the page.

Do anyone knows how to prevent this from occurring?

I have tried using thread interrupt where I reset the mutex but no joy.

EDITED:

isStopped is volatile.

enter image description here

  • 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-25T13:24:09+00:00Added an answer on May 25, 2026 at 1:24 pm

    To follow on from @Jordão’s answer, the isStopped variable should be per thread. I would recommend using something like an AtomicBoolean and changing your thread code to be approximately:

    public AtomicBoolean testListener() {
        final AtomicBoolean isStopped = new AtomicBoolean(false);
        Thread listener = new Thread(new Runnable() {
            public void run() {
                while(!isStopped.get()) {
                    ...
                }
            }
        });
        listener.setName("Test-Server-Daemon");
        listener.setDaemon(true);
        listener.start();
        return isStopped;
    }
    

    Then back in your page controller you can do:

    AtomicBoolean isStopped = testListener();
    // do the page stuff
    ...
    // when done stop the thread
    isStopped.set(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app which acts almost like a daemon. Sometimes it quits on
I have a daemon thread which wakes up at a specified interval to do
I have written a simple Java dispatcher with a daemon thread to handle the
I have a simple Perl script which runs as a Linux daemon using an
I have a thread running under tomcat which creates a HttpUrlConnection and reads it
I have created a simple Java application which has a JFrame and few JButtons.
I have a daemon app written in C and is currently running with no
So I have a daemon running on a Linux system, and I want to
I am going to have a daemon that will run on a FreeBSD server,
I have a Solaris daemon written in Java6. Clients can connect to it using

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.