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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:30:39+00:00 2026-06-16T02:30:39+00:00

I’m looking at using the java.util.concurrent package for a simple polling class. Am a

  • 0

I’m looking at using the java.util.concurrent package for a simple polling class. Am a bit perplexed by the range of classes, interfaces and methods available for doing this so would appreciate some guidance. Here are my thoughts so far:

The first decision to make is how to instantiate the class for managing a schedule. There are a few possible options, e.g:

ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =
    new ScheduledThreadPoolExecutor(corePoolSize)

…or…

ScheduledExecutorService scheduledExecutorService =
    Executors.newSingleThreadScheduledExecutor();

…or…

ScheduledExecutorService scheduledExecutorService =
    Executors.newScheduledThreadPool(corePoolSize);

Am leaning towards the last one but wondering what a sensible corePoolSize would be – perhaps 1 to keep things simple?

EDIT: In the end I found most benefit in using the top method (i.e. directly instantiate ScheduledThreadPoolExecutor). ThreadPoolExecutor provides getActiveCount() for obtaining the number of active threads. This allowed me to implement a pause() method that waits until the pause has actually taken effect – see discussion.

The next decision is whether to call scheduleAtFixedRate or scheduleWithFixedDelay. Am leaning towards scheduleWithFixedDelay() since the polling regularity isn’t all that important and I don’t like the idea of multiple polls occurring in quick succession after a bottleneck.

But here’s the question: Would it be OK/advisable to use a single class that both starts the polling and represents the thread? E.g:

public class Poller extends Thread {
    @Override
    public void run() {
        ...
    }

    public void startPolling() {
        ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
        exec.scheduleWithFixedDelay(this, 0, 5000, TimeUnit.MILLISECONDS);
    }
}

The main part I’m not sure about here is the first scheduleWithFixedDelay() parameter: Will a new instance of this class be instantiated for each execution? Otherwise it surely wouldn’t work since run() can’t be called on the same Thread instance twice?

  • 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-06-16T02:30:40+00:00Added an answer on June 16, 2026 at 2:30 am

    As others have commented, all you need to change is Thread to Runnable. You might want to add some safe guard so that there is not multiple tasks being run.

    public class Poller implements Runnable {
        final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
        Future future = null;
    
        @Override
        public void run() {
            ...
        }
    
        public void startPolling() {
            if (future != null && !future.isDone()) {
               future.cancel(true); // stop before restarting
               // or
               return; // already running
            }
            future = exec.scheduleWithFixedDelay(this, 0, 5000, TimeUnit.MILLISECONDS);
        }
    }
    

    BTW: If you have Java 5.0, the run() method does not have the @Override. For Java 6+, you should have the @Override.

    Will a new instance of this class be instantiated for each execution?

    A new instances is created when you use the new operator.

    Otherwise it surely wouldn’t work run() surely can’t be called on the same Thread instance twice?

    It can work because the use of Thread in your code is confusing as the thread you create is not used. It just needs to be Runnable.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.