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 7084053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:15:14+00:00 2026-05-28T07:15:14+00:00

I have a poller implemented from Runnable. The poller passes itself into a ScheduledExecutorService.

  • 0

I have a poller implemented from Runnable. The poller passes itself into a ScheduledExecutorService. Since I need different delay time depending on the tasks, I use ‘schedule’ instead of ‘scheduleWithFixedDelay’, which got a problem…

Unfortunately, this poller can’t shut down properly…

The log shows that:
at beginning, ‘main’ thread schedules a poller;
after that, ‘poller18’ (or whatever thread id) thread schedules the following pollers.

When destroyed,
‘main’ thread calls destroy, set the flag to true, and then call poller.shutdown;
but ‘poller18’ thread never see that flag (it is always false in schedulePoller), so it will keep scheduling next pollers.

My questions are:
1. isShuttingDown is a private field in the class. I thought it would be shared by the threads since it is not ThreadLocal. Why isn’t it?
2. in this situation, is there any other way poller18 can be notified that poller is shutting down?

class Poller implements Runnable {
    private boolean isShuttingDown = false;
    private ScheduledExecutorService poller = null;

    @PostConstruct
    protected synchronized void start() {
      if (enabled && poller == null) {
          poller = Executors.newScheduledThreadPool(1);
          schedulePoller(1);
      }
   }

   protected synchronized void schedulePoller(final long period) {
      if (poller() == null || poller().isShutdown() || this.isShuttingDown) {
          return;
      }
      LOGGER.info("schedule a new poller");
      poller().schedule(this,
              period,
              TimeUnit.MILLISECONDS);
   }

   public void run() {
      ... do work ...
      if (more work)
           schedulePoller(1);
      else
           schedulePoller(10);
   }

   public void destroy() {
      this.isShuttingDown = true;
      poller.shutdown();
      while (!poller.awaitTermination(SHUTDOWN_WAIT_SECONDS, TimeUnit.SECONDS)) {
          LOGGER.info("Waiting for remaining tasks to finish.");
          poller.shutdown();
      }
      LOGGER.info("All remaining tasks have finished.");
   }
}

And in spring config, I set destroy_method to destroy().

Many thanks! Please let me know if any confusions in my description.

  • 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-28T07:15:15+00:00Added an answer on May 28, 2026 at 7:15 am

    When accessing the shared state (isShuttingDown), whether it is read or write access, you need to always hold the same lock in order to have consistent shared state between threads.
    destroy() method is not synchronized and therefore does not hold a the same object lock(the implicit lock on the current/”this” class instance), while it changes the shared state variable.
    This is most probably why isShuttingDown is not seen correctly by all threads…

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

Sidebar

Related Questions

Have just started converting an existing job tracking system into an ASP.NET MVC application.
I have this: <si:poller max-messages-per-poll=10 id=defaultPoller default=true> <si:interval-trigger interval=5000/> </si:poller> <si:channel id=emailIn/> <si:channel id=emailOut/>
i have the value polled from the db and if the user want to
I am getting started with AOP for the first time. I have my first
I have an XML file that is loaded into an XElement. I want to
I am using cwac-Location Poller (from here ) to constantly poll user location and
I have an application that is a data poller/updater and there are tons of
I need to have functionality that would essentially emulate user creating a port forwrding
I have file channel adapter which need to listen to a directory in specfied
Have I understod the following right? font-family:sans-serif; Above will result in the default sans-serif

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.